* { box-sizing: border-box; }

/* FOUC fix — пока кастомные элементы Shoelace не зарегистрированы, скрываем
   их чтобы юзер не видел unstyled HTML (голые кнопки/инпуты, прыгающий лейаут).
   После customElements.define(...) — :defined матчится, элементы показываются. */
sl-button:not(:defined),
sl-icon-button:not(:defined),
sl-icon:not(:defined),
sl-input:not(:defined),
sl-textarea:not(:defined),
sl-tag:not(:defined),
sl-switch:not(:defined),
sl-dialog:not(:defined),
sl-drawer:not(:defined),
sl-spinner:not(:defined),
sl-alert:not(:defined) {
  visibility: hidden;
}

:root {
  --bg: #f5f5f7;
  --surface: #fff;
  --border: #e5e5e7;
  --text: #1d1d1f;
  --muted: #86868b;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --user-bg: #0071e3;
  --user-fg: #fff;
  --bot-bg: #f0f0f3;
  --bot-fg: #1d1d1f;
  --error: #d12c2c;
  --error-bg: #fee;
  --code-bg: #1d1d1f;
  --code-fg: #f5f5f7;

  /* Shoelace theme overrides — связываем их токены с нашими, чтобы
     компоненты <sl-*> сразу выглядели в нашем iOS-стиле */
  --sl-color-primary-50:  #e7f1ff;
  --sl-color-primary-100: #cfe3ff;
  --sl-color-primary-200: #9fc8ff;
  --sl-color-primary-300: #6faaff;
  --sl-color-primary-400: #3f8cff;
  --sl-color-primary-500: var(--accent);
  --sl-color-primary-600: var(--accent);
  --sl-color-primary-700: var(--accent-hover);
  --sl-color-primary-800: #0061c3;
  --sl-color-primary-900: #00498f;
  --sl-color-primary-950: #002f5e;
  /* Radius scale — синхронизируем с нашими паттернами */
  --sl-border-radius-small: 6px;
  --sl-border-radius-medium: 10px;
  --sl-border-radius-large: 14px;
  --sl-border-radius-x-large: 16px;
  /* Spacing scale на сетке 4 */
  --sl-spacing-2x-small: 4px;
  --sl-spacing-x-small: 6px;
  --sl-spacing-small: 8px;
  --sl-spacing-medium: 12px;
  --sl-spacing-large: 16px;
  --sl-spacing-x-large: 24px;
  /* Fonts — наследуем системные */
  --sl-font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                  "Helvetica Neue", Arial, sans-serif;
  --sl-input-border-radius-small: 8px;
  --sl-input-border-radius-medium: 10px;
  --sl-input-border-radius-large: 12px;
  --sl-input-height-small: 32px;
  --sl-input-height-medium: 40px;
  --sl-input-height-large: 48px;
  /* Тёмная тема перебивает через .sl-theme-dark класс на html */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1e;
    --surface: #2c2c2e;
    --border: #3a3a3c;
    --text: #f5f5f7;
    --muted: #98989d;
    --user-bg: #0a84ff;
    --bot-bg: #3a3a3c;
    --bot-fg: #f5f5f7;
    --error-bg: #3a1f1f;
  }
}

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
/* Stats page нужен скролл (длинная страница), переопределяем глобал */
html:has(body.stats-body),
body.stats-body {
  height: auto;
  min-height: 100%;
  overflow: auto;
  overscroll-behavior: auto;
  background: var(--bg);
}

body {
  margin: 0;
  font: 15px/1.45 -apple-system, "Segoe UI", "SF Pro Text", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body.centered {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.card {
  background: var(--surface);
  border-radius: 12px;
  padding: 32px 28px;
  width: min(320px, 88vw);
  box-shadow: 0 4px 20px rgba(0,0,0,.08);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (max-width: 480px) {
  .card {
    padding: 28px 20px;
    border-radius: 16px;
    width: 90vw;
  }
  .card h1 { font-size: 32px; }
  .card input, .card button { font-size: 16px; padding: 12px 16px; }
}

.card h1 {
  margin: 0 0 8px;
  font-size: 28px;
  text-align: center;
  font-weight: 600;
}

input, button, textarea {
  font: inherit;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
  background: var(--surface);
  color: var(--text);
}

input:focus, textarea:focus { border-color: var(--accent); }

button {
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  padding: 10px 18px;
  font-weight: 500;
  transition: background .15s;
}

button:hover:not(:disabled) { background: var(--accent-hover); }
button:disabled { opacity: .5; cursor: default; }

.error {
  color: var(--error);
  font-size: 13px;
  text-align: center;
}

/* ── App layout ── */

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;
  height: 100dvh;
  height: var(--app-h, 100dvh);
  background: var(--surface);
  overflow: hidden;
}

.sidebar {
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
}
.mobile-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Sidebar buttons — sl-button с width: 100% */
.sidebar sl-button.sidebar-btn { width: 100%; }
.sidebar sl-button.sidebar-btn::part(base) {
  width: 100%;
  justify-content: flex-start;
}

/* Sidebar layout: header + scroll-zone + bottom + footer */
.sidebar { display: flex; flex-direction: column; }
.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.sidebar-bottom {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
  border-top: 1px solid var(--border);
}

/* Chat groups — sl-details со сводным заголовком */
sl-details.chat-group::part(base) {
  border: 0;
  background: transparent;
  border-radius: 8px;
}
sl-details.chat-group::part(header) {
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}
sl-details.chat-group::part(header):hover { background: var(--bot-bg); }
sl-details.chat-group::part(content) {
  padding: 4px 4px 8px 4px;
  overflow: hidden;
}
.group-summary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}
.group-summary sl-icon { font-size: 16px; }
.group-count {
  margin-left: auto;
  background: var(--bot-bg);
  color: var(--muted);
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 10px;
  font-weight: 500;
}

/* Project actions row (KB + Settings + New chat) */
.project-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 6px;
  padding: 0 4px;
}
.project-actions sl-button { width: 100%; }
.project-actions sl-button::part(base) {
  width: 100%;
  justify-content: flex-start;
  font-size: 12px;
}

.project-chats { display: flex; flex-direction: column; gap: 2px; min-width: 0; overflow: hidden; }
.proj-empty {
  font-size: 12px;
  color: var(--muted);
  padding: 8px 12px;
  text-align: center;
  font-style: italic;
}

.new-project-btn { margin: 8px 4px 0; }

/* KB modal — переиспользует library стили + свои штрихи */
sl-dialog.kb-modal::part(panel) {
  max-width: 720px;
  width: 100%;
  height: min(92vh, 900px);
  max-height: 92vh;
}
sl-dialog.kb-modal::part(body) { padding: 0; display: flex; flex-direction: column; }
@media (max-width: 600px) {
  sl-dialog.kb-modal::part(panel) {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
  }
}
.kb-add-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.kb-add-row sl-button { flex: 1; min-width: 140px; }
.kb-reader-source {
  padding: 8px 12px;
  background: var(--bot-bg);
  border-radius: 8px;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.kb-reader-source:empty { display: none; }
.kb-reader-source a { color: var(--accent); text-decoration: none; word-break: break-all; }

/* Project edit modal — icon/color pickers */
sl-dialog.proj-edit-modal::part(panel) { max-width: 500px; }
sl-dialog.proj-edit-modal::part(body) { overflow-x: hidden; }
@media (max-width: 600px) {
  sl-dialog.proj-edit-modal::part(panel) {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
  }
  sl-dialog.proj-edit-modal::part(body) {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
  }
}
.proj-edit-modal sl-input,
.proj-edit-modal sl-textarea {
  margin-bottom: 10px;
  display: block;
  width: 100%;
  max-width: 100%;
}

/* Профиль / стиль общения */
sl-dialog.profile-modal::part(panel) { max-width: 460px; }
.profile-modal sl-input { display: block; width: 100%; }
.profile-hint {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
  line-height: 1.4;
}
.profile-modal sl-radio { display: block; margin-top: 6px; }
.proj-picker-section { margin-bottom: 16px; }
.proj-picker-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
  font-weight: 500;
}
.proj-icon-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
}
.proj-icon-cell {
  aspect-ratio: 1;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 16px;
  transition: all .15s;
}
.proj-icon-cell:hover { border-color: var(--accent); color: var(--accent); }
.proj-icon-cell.on {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.proj-color-grid {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  min-width: 0;
}
.proj-color-cell {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  transition: transform .1s;
  flex-shrink: 0;
}
.proj-color-cell:hover { transform: scale(1.1); }
.proj-color-cell.on { border-color: var(--text); }
.proj-picker-section { min-width: 0; }
.proj-icon-grid { min-width: 0; }
@media (max-width: 600px) {
  .proj-icon-grid { grid-template-columns: repeat(6, 1fr); }
  .proj-color-cell { width: 32px; height: 32px; }
}

.sessions-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.session-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 4px;
  transition: background .1s;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
.session-item .session-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  overflow: hidden;
}
.session-item .session-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-item:hover { background: var(--border); }
.session-item.active { background: var(--accent); color: #fff; }
.session-item .meta {
  display: block;
  font-size: 11px;
  opacity: .7;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-item .delete {
  visibility: hidden;
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 2px 6px;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
}
.session-item:hover .delete { visibility: visible; }
.session-item.active .delete { color: rgba(255,255,255,.7); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.sidebar-footer .username { color: var(--muted); }
.sidebar-footer form button,
.sidebar-footer .footer-actions button {
  background: transparent;
  color: var(--muted);
  padding: 4px 8px;
  font-size: 13px;
  border: none;
  cursor: pointer;
}
.sidebar-footer form button:hover,
.sidebar-footer .footer-actions button:hover { color: var(--text); background: transparent; }
.sidebar-footer .footer-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.sidebar-footer .footer-actions button { font-size: 15px; }

.main {
  display: grid;
  grid-template-rows: 1fr auto;
  overflow: hidden;
}

#messages {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
}

.msg {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.5;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-fg);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.msg.bot {
  align-self: flex-start;
  background: var(--bot-bg);
  color: var(--bot-fg);
  border-bottom-left-radius: 4px;
  max-width: 85%;
}

.msg.system {
  align-self: center;
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-style: normal;
}

/* Empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 20px;
  gap: 12px;
  color: var(--muted);
}
.empty-state img {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  margin-bottom: 8px;
}
.empty-state h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  color: var(--text);
}
.empty-state p {
  margin: 0;
  max-width: 440px;
  line-height: 1.5;
  font-size: 14px;
}

.msg.error {
  align-self: center;
  background: var(--error-bg);
  color: var(--error);
  font-size: 13px;
}

/* Markdown styling inside .msg.bot */
.msg.bot > *:first-child { margin-top: 0; }
.msg.bot > *:last-child { margin-bottom: 0; }
.msg.bot p { margin: 8px 0; }
.msg.bot h1, .msg.bot h2, .msg.bot h3 {
  margin: 14px 0 6px;
  font-weight: 600;
}
.msg.bot h1 { font-size: 1.3em; }
.msg.bot h2 { font-size: 1.15em; }
.msg.bot h3 { font-size: 1.05em; }
.msg.bot ul, .msg.bot ol {
  margin: 6px 0;
  padding-left: 24px;
}
.msg.bot li { margin: 3px 0; }
.msg.bot blockquote {
  border-left: 3px solid var(--muted);
  margin: 6px 0;
  padding: 4px 12px;
  color: var(--muted);
}
.msg.bot a { color: var(--accent); }
.msg.bot table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}
.msg.bot th, .msg.bot td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.msg.bot th { background: rgba(0,0,0,.05); }
.msg.bot pre {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 8px 0;
  font-size: 13px;
  line-height: 1.45;
}
.msg.bot pre code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}
.msg.bot code {
  background: rgba(0,0,0,.08);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 90%;
}

@media (prefers-color-scheme: dark) {
  .msg.bot code { background: rgba(255,255,255,.1); }
}

/* Footer / input */

footer {
  border-top: 1px solid var(--border);
  padding: 10px 14px;
  padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--surface);
}

#input {
  flex: 1 1 auto;
  resize: none;
  min-height: 40px;
  max-height: 200px;
  border-radius: 20px;
  padding: 10px 14px;
  min-width: 0;
}

footer button {
  flex: 0 0 auto;
}

/* Footer кнопки — sl-button[circle]. Размер через Shoelace --sl-input-height-medium */
footer sl-button.plus-btn,
footer sl-button.mic-btn,
footer sl-button#send {
  flex-shrink: 0;
  --sl-input-height-medium: 40px;
}
@media (max-width: 720px) {
  footer { padding: 8px 10px; padding-bottom: max(8px, env(safe-area-inset-bottom, 8px)); gap: 6px; }
  footer sl-button.plus-btn,
  footer sl-button.mic-btn,
  footer sl-button#send {
    --sl-input-height-medium: 38px;
  }
  #input { min-height: 38px; font-size: 16px; padding: 9px 14px; }
}

/* Plus-menu — popup above the plus button with "Прикрепить файл" / "Шаблоны" */
.plus-menu {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
  min-width: 220px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18);
  z-index: 20;
}
.plus-menu.show { display: block; }
.plus-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 10px 14px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
}
.plus-menu-item:hover { background: var(--bot-bg); }
footer { position: relative; }  /* anchor for .plus-menu absolute positioning */

/* Аглая Петровна — wife of Жан, speaks on technical errors */
.msg-with-avatar {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  max-width: 85%;
  align-self: flex-start;
}
.msg-with-avatar .msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid var(--border);
}
.msg-with-avatar .msg-body { flex: 1; min-width: 0; }
.msg-with-avatar .msg-name {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 2px;
  padding-left: 4px;
  font-weight: 500;
}
.msg-with-avatar .aglaya-bubble {
  margin: 0;
  max-width: 100%;
  background: linear-gradient(135deg, rgba(180,120,90,.12), rgba(140,90,60,.08));
  border: 1px solid rgba(180,120,90,.25);
}

/* New message pill */
.new-pill {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .25s;
  z-index: 5;
}
.new-pill.show {
  opacity: 1;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.main { position: relative; }

/* Side panel (slide-in from right) */
.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 380px;
  max-width: 100%;
  height: 100vh;
  height: 100dvh;
  height: var(--app-h, 100dvh);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -2px 0 20px rgba(0,0,0,.12);
  transform: translateX(100%);
  transition: transform .25s ease-out;
  z-index: 60;
  display: flex;
  flex-direction: column;
  visibility: hidden;
}
.side-panel.show { visibility: visible; }
.side-panel.show { transform: translateX(0); }
.panel-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 55;
}
.panel-backdrop.show { display: block; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-header h2 { margin: 0; font-size: 17px; }
/* .panel-close теперь sl-icon-button — стили общие, см. ниже sl-icon-button.modal-close */
.panel-body {
  overflow-y: auto;
  padding: 14px 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}
@media (max-width: 480px) {
  .side-panel { width: 100%; }
}

/* Top bell — sl-icon-button с round-shape, фикс позиции справа сверху */
sl-icon-button.top-bell {
  position: absolute;
  top: 8px;
  right: 10px;
  font-size: 18px;
  z-index: 8;
}
sl-icon-button.top-bell::part(base) {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
sl-icon-button.top-bell.has-unread::part(base)::after {
  content: "";
  position: absolute;
  top: 4px;
  right: 4px;
  width: 8px;
  height: 8px;
  background: #ff453a;
  border-radius: 50%;
  border: 2px solid var(--surface);
}
@media (max-width: 720px) {
  sl-icon-button.top-bell { top: 8px; right: 10px; }
  sl-icon-button.top-bell::part(base) { width: 36px; height: 36px; }
}

/* Typing indicator (inside bubble — flashes during streaming) */
.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 6px 4px;
}
.typing span {
  width: 6px; height: 6px;
  background: var(--muted);
  border-radius: 50%;
  animation: typing 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Persistent thinking bar above the input — стоит пока task не закрылся,
   даже если стрим уже идёт. Гарантированно видно даже при коротких ответах
   и при tool-use (когда стрима ещё нет, но Жан работает). */
.thinking-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: var(--bot-bg);
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--muted);
}
.thinking-bar.show { display: flex; }
.thinking-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
}
.thinking-text { flex-shrink: 0; }
.thinking-dots {
  display: inline-flex;
  gap: 3px;
  align-items: center;
}
.thinking-dots span {
  width: 4px; height: 4px;
  background: var(--muted);
  border-radius: 50%;
  animation: typing 1.2s infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: .15s; }
.thinking-dots span:nth-child(3) { animation-delay: .3s; }

/* Sidebar spinner on chats with active task */
.session-item .session-thinking {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  margin-left: 6px;
  animation: pulse 1.4s infinite;
  vertical-align: middle;
  flex-shrink: 0;
}
@keyframes pulse {
  0%, 100% { opacity: .35; transform: scale(.9); }
  50% { opacity: 1; transform: scale(1.1); }
}

/* Pinned templates — strip above input showing attached templates for this chat */
.pinned-templates {
  display: none;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px 14px;
  background: var(--bot-bg);
  border-top: 1px solid var(--border);
}
.pinned-templates.show { display: flex; }
/* pinned-tpl-chip — теперь sl-tag[removable]. max-width чтоб длинные имена обрезались */
sl-tag.pinned-tpl-chip { max-width: 240px; }
sl-tag.pinned-tpl-chip::part(base) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Templates picker — sl-dialog с custom header внутри body */
sl-dialog.tpl-modal::part(panel) { max-width: 560px; width: 100%; max-height: 86vh; }
sl-dialog.tpl-modal::part(body) { padding: 0; display: flex; flex-direction: column; }
.tpl-body { flex: 1; overflow-y: auto; }
.tpl-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
sl-button.tpl-add-btn { flex: 1; min-width: 140px; }
.tpl-list { display: flex; flex-direction: column; gap: 6px; }
.tpl-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color .1s, background .1s;
}
.tpl-item:hover { border-color: var(--accent); background: var(--bot-bg); }
.tpl-info { flex: 1; min-width: 0; }
.tpl-name {
  font-size: 14px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
  overflow: hidden;
}
sl-icon.tpl-pin-mark { color: var(--accent); }
sl-tag.tpl-orig-badge { margin-left: auto; }
.tpl-meta {
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
  display: flex;
  gap: 4px;
  align-items: center;
  flex-wrap: wrap;
}
sl-button.tpl-attach-btn { flex-shrink: 0; }

/* Reader/editor view inside templates modal */
#tpl-reader-view { flex-direction: column; gap: 10px; flex: 1; }
/* tpl-reader inputs — sl-input. Имя жирнее для визуальной важности */
sl-input.tpl-reader-name-input::part(input) { font-weight: 600; font-size: 15px; }
.tpl-reader-orig {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bot-bg);
  border-radius: 8px;
  font-size: 12px;
  color: var(--muted);
}
.tpl-reader-orig-icon { font-size: 16px; }
.tpl-reader-orig-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tpl-reader-orig-dl {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
  white-space: nowrap;
}
/* tpl-reader content — sl-textarea с monospace для markdown */
sl-textarea.tpl-reader-content-edit { flex: 1; }
sl-textarea.tpl-reader-content-edit::part(textarea) {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
}
.tpl-reader-bottom {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
sl-button.tpl-reader-btn { flex: 1; min-width: 120px; }
/* Bottom-sheet на мобиле — sl-dialog с custom positioning через ::part */
@media (max-width: 600px) {
  sl-dialog.tpl-modal::part(panel) {
    width: 100%;
    max-width: 100%;
    max-height: 86dvh;
    margin: 0;
    align-self: end;
    border-radius: 16px 16px 0 0;
  }
  sl-dialog.tpl-modal::part(overlay) {
    align-items: flex-end;
  }
}

/* (тулбар-кнопки templates/attach теперь спрятаны в .plus-menu — см. выше) */

/* ── Universal dialog (showPrompt / showConfirm / showAlert) ── */
/* Help modal — sl-dialog с разделами sl-details (accordion) */
sl-dialog#help-modal::part(panel) { max-width: 640px; }
sl-dialog#help-modal sl-details {
  margin-bottom: 8px;
}
sl-dialog#help-modal sl-details::part(summary) {
  font-weight: 600;
  font-size: 14px;
}
sl-dialog#help-modal sl-details ul {
  margin: 8px 0;
  padding-left: 20px;
}
sl-dialog#help-modal sl-details li {
  margin-bottom: 6px;
  line-height: 1.5;
  font-size: 14px;
}
.help-intro {
  margin: 0 0 16px;
  font-size: 14px;
  color: var(--text);
  line-height: 1.5;
}
.help-note {
  margin: 10px 0 0;
  padding: 8px 12px;
  background: var(--bot-bg);
  border-radius: 8px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}
.help-footer {
  margin-top: 20px;
  text-align: center;
}

/* Universal dialog — sl-dialog. Higher z-index чтобы перекрывать другие
   модалки (например при создании шаблона из template-picker'а). */
sl-dialog#dialog-modal { --sl-z-index-dialog: 1100; }
sl-dialog#dialog-modal::part(panel) { max-width: 440px; }
.dialog-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}
.dialog-title:empty { display: none; }
.dialog-message {
  color: var(--text);
  font-size: 14px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.dialog-message:empty { display: none; }
sl-input.dialog-input,
sl-textarea.dialog-textarea { margin-top: 10px; }

/* Progress dialog */
sl-dialog#progress-modal { --sl-z-index-dialog: 1150; }
sl-dialog#progress-modal::part(panel) { max-width: 340px; }
sl-dialog#progress-modal::part(close-button) { display: none; }
.progress-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 8px 0;
}
.progress-text {
  color: var(--text);
  font-size: 14px;
  text-align: center;
  line-height: 1.4;
}

/* Lib-move dialog — узкий */
sl-dialog#lib-move-modal::part(panel) { max-width: 480px; }

/* Mobile */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 280px;
    transform: translateX(-100%);
    transition: transform .25s;
    z-index: 10;
    box-shadow: 2px 0 16px rgba(0,0,0,.15);
  }
  .sidebar.open { transform: translateX(0); }
  .hamburger {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--bot-bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 18px;
    cursor: pointer;
    font-weight: 500;
  }
  .hamburger::after { content: "Чаты"; font-size: 14px; }
  .msg { max-width: 90%; }
}
.hamburger { display: none; }

.mobile-header {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-right: 56px;  /* room for bell button */
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
.mobile-header-spacer { flex: 1; }
@media (max-width: 720px) {
  .mobile-header { display: flex; }
  .main { grid-template-rows: auto 1fr auto; }
}
.mobile-header .title { font-weight: 600; }

.main {
  min-height: 0;
}
footer {
  flex-shrink: 0;
}

.backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 9;
}
.backdrop.show { display: block; }

/* ── Attachments ── */

/* Mic-button recording state — поверх sl-button через ::part(base) */
sl-button.mic-btn.recording::part(base) {
  background: #ff453a;
  color: #fff;
  border-color: #ff453a;
  animation: pulse 1s ease-in-out infinite;
}
sl-button.mic-btn.recording sl-icon { color: #fff; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 69, 58, .5); }
  50% { box-shadow: 0 0 0 8px rgba(255, 69, 58, 0); }
}

#attachments-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 20px;
}
#attachments-bar:empty { display: none; }
#attachments-bar + footer { padding-top: 8px; }

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: var(--bot-bg);
  border-radius: 8px;
  font-size: 12px;
  color: var(--text);
  text-decoration: none;
  max-width: 280px;
}
.file-chip .ico { font-size: 14px; }
.file-chip .fname {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}
.file-chip.pending { opacity: .7; }
.file-chip.uploaded { opacity: 1; }
.file-chip.error { background: var(--error-bg); color: var(--error); }
.file-chip .rm {
  background: transparent;
  color: var(--muted);
  border: none;
  padding: 0 2px;
  cursor: pointer;
  font-size: 14px;
}

.attachments-in-msg {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
  min-width: 0;
}

.image-preview {
  position: relative;
  display: inline-block;
  cursor: zoom-in;
  border-radius: 10px;
  overflow: hidden;
  max-width: 320px;
  background: var(--bot-bg);
}
.image-preview img {
  display: block;
  max-width: 100%;
  max-height: 320px;
  object-fit: cover;
}
.image-preview .image-dl {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0,0,0,.6);
  color: #fff;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-size: 14px;
  opacity: 0;
  transition: opacity .15s;
}
.image-preview:hover .image-dl { opacity: 1; }
@media (hover: none) {
  .image-preview .image-dl { opacity: 1; }
}

/* Video player — width привязана к viewport, не к parent flex. На мобиле
   capped at "85vw - 30px" = bubble content area. На десктопе capped at 320.
   НЕ полагаемся на flex/intrinsic-sizing parent цепочку (она ненадёжна
   на mobile Chrome — minimum sizing для controls и др. quirks). */
.video-preview {
  position: relative;
  display: block;
  width: min(320px, calc(85vw - 30px));
  border-radius: 10px;
  overflow: hidden;
  background: #000;
}
.video-preview video {
  display: block;
  width: 100%;
  height: auto;
  max-height: 360px;
  object-fit: contain;
}
.video-preview .image-dl {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0,0,0,.6);
  color: #fff;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-size: 14px;
  opacity: 0;
  transition: opacity .15s;
  z-index: 2;
}
.video-preview:hover .image-dl { opacity: 1; }
@media (hover: none) { .video-preview .image-dl { opacity: 1; } }

/* Audio player in chat */
.audio-preview {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--bot-bg);
  max-width: 360px;
}
.audio-preview audio {
  width: 100%;
  min-width: 280px;
  height: 36px;
}
.audio-preview .audio-name {
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.audio-preview .audio-name:hover { color: var(--text); }

/* Slide-strip preview for .pptx / .pdf — vertical PNG strip, scrollable. */
.slide-strip {
  display: block;
  width: min(480px, calc(85vw - 30px));
  border-radius: 12px;
  overflow: hidden;
  background: var(--bot-bg);
  border: 1px solid rgba(0,0,0,.08);
}
.strip-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(0,0,0,.04);
  font-size: 13px;
  border-bottom: 1px solid rgba(0,0,0,.06);
}
.strip-header .strip-ico { font-size: 16px; }
.strip-header .strip-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.strip-header .strip-dl {
  color: var(--muted);
  text-decoration: none;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 6px;
  white-space: nowrap;
}
.strip-header .strip-dl:hover { color: var(--text); background: rgba(0,0,0,.06); }
.strip-slides {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  max-height: 540px;
  overflow-y: auto;
  background: rgba(0,0,0,.04);
}
.strip-slide {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 4px;
  background: #fff;
}
.strip-status {
  text-align: center;
  padding: 24px 12px;
  color: var(--muted);
  font-size: 13px;
}

/* Image gallery: smaller thumbs when many images in one reply */
.images-row.many-images .image-preview {
  max-width: 180px;
}
.images-row.many-images .image-preview img {
  max-height: 180px;
}

/* Lightbox */
.lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.92);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 20px;
  cursor: zoom-out;
}
.lightbox.show { display: flex; }
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
}
.lightbox #lightbox-close {
  position: fixed;
  top: 12px;
  right: 12px;
  background: rgba(255,255,255,.15);
  color: #fff;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.lightbox #lightbox-dl {
  position: fixed;
  top: 12px;
  right: 60px;
  background: rgba(255,255,255,.15);
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-size: 18px;
}
.msg.user .attachments-in-msg .file-chip {
  background: rgba(255,255,255,.2);
  color: #fff;
}
.msg.bot .attachments-in-msg .file-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
}
.msg.bot .attachments-in-msg .file-chip:hover { border-color: var(--accent); }

/* Drop overlay */
.drop-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 113, 227, .3);
  z-index: 100;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.drop-overlay.show { display: flex; }
.drop-inner {
  background: rgba(0,0,0,.85);
  color: #fff;
  padding: 24px 40px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 500;
}

/* Library modal */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 50;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal.show { display: flex; }
.modal-content {
  background: var(--surface);
  border-radius: 14px;
  width: 100%;
  max-width: 600px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 17px; }
/* sl-icon-button.modal-close (+ .lib-back, .panel-close) — Shoelace компонент.
   font-size управляет размером иконки внутри. Hover уже встроен в Shoelace. */
sl-icon-button.modal-close,
sl-icon-button.lib-back,
sl-icon-button.panel-close {
  font-size: 18px;
  --sl-color-neutral-700: var(--muted);  /* default цвет иконки */
}
sl-icon-button.modal-close::part(base):hover,
sl-icon-button.lib-back::part(base):hover,
sl-icon-button.panel-close::part(base):hover {
  color: var(--text);
  background: var(--bot-bg);
  border-radius: 50%;
}
.modal-body {
  overflow-y: auto;
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.lib-section h3 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .5px;
  font-weight: 600;
}
.lib-list { display: flex; flex-direction: column; gap: 6px; }
.lib-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 12px 0;
  text-align: center;
}

.lib-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 8px;
}
.lib-icon { font-size: 20px; flex-shrink: 0; }
.lib-info { flex: 1; min-width: 0; }
.lib-name {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lib-meta { font-size: 11px; color: var(--muted); }
.lib-actions { display: flex; gap: 4px; flex-shrink: 0; }
.lib-act {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 16px;
  text-decoration: none;
  line-height: 1;
  display: inline-flex;
  align-items: center;
}
.lib-act:hover { background: var(--border); color: var(--text); }
.lib-act.download { color: var(--accent); }
.lib-act.delete:hover { color: var(--error); }

/* Library v2 — sl-dialog с custom header (no-header атрибут) */
sl-dialog.lib-modal::part(panel) {
  max-width: 720px;
  width: 100%;
  height: min(92vh, 900px);
  max-height: 92vh;
}
sl-dialog.lib-modal::part(body) {
  padding: 0;
  display: flex;
  flex-direction: column;
}
/* Fullscreen library on mobile — больше места под чтение */
@media (max-width: 600px) {
  sl-dialog.lib-modal::part(panel) {
    width: 100%;
    max-width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-radius: 0;
  }
  sl-dialog.lib-modal .lib-header {
    padding-top: max(10px, env(safe-area-inset-top));
  }
}
.lib-header { position: relative; }
/* .lib-back теперь sl-icon-button — стили выше в блоке sl-icon-button.modal-close */
.lib-body { flex: 1; overflow-y: auto; padding: 12px 16px; }
.lib-view { display: block; }
#lib-reader-view.lib-view,
#kb-reader-view.lib-view { flex-direction: column; height: 100%; }

sl-button.lib-create-folder { margin-bottom: 12px; }

.lib-folder-list { display: flex; flex-direction: column; gap: 6px; }
.lib-folder {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: var(--text);
  font-size: 15px;
}
.lib-folder:hover { border-color: var(--accent); background: var(--bot-bg); }
.lib-folder-icon { font-size: 20px; }
.lib-folder-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* .lib-count теперь sl-tag — настройки через Shoelace */

/* Search bar across the top of the library modal */
/* Search bar в шапке библиотеки — sl-input внутри */
.lib-search-bar {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
}
.lib-search-bar sl-input { width: 100%; }

/* Tag pills on file items + reader */
.lib-file-tags {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  margin-top: 4px;
}
/* .lib-tag-pill теперь sl-tag — кликабельные с [data-tag] меняют курсор */
sl-tag.lib-tag-pill[data-tag] { cursor: pointer; }
sl-tag.lib-tag-pill[data-tag]:hover::part(base) {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.lib-breadcrumb {
  color: var(--accent);
  margin-right: 6px;
}

.lib-reader-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px 0 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
.lib-reader-tags:empty { display: none; }

/* (lib-move-content теперь sl-dialog со стилями выше) */

.lib-old-media {
  margin-top: 20px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.lib-old-media summary {
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  padding: 6px 0;
}
.lib-old-media summary:hover { color: var(--text); }

.lib-file-list { display: flex; flex-direction: column; gap: 4px; }
.lib-file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: var(--text);
}
.lib-file-item:hover { border-color: var(--accent); background: var(--bot-bg); }
.lib-file-icon { font-size: 18px; }
.lib-file-info { flex: 1; min-width: 0; }
.lib-file-name {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lib-file-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }

.lib-reader-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0 10px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}
/* lib-reader-name — sl-input[readonly], имя файла. Без borders, жирный */
sl-input.lib-reader-name { flex: 1; min-width: 0; }
sl-input.lib-reader-name::part(form-control) { width: 100%; }
sl-input.lib-reader-name::part(input) {
  font-weight: 600;
  background: transparent;
}
sl-input.lib-reader-name::part(base) {
  border: 0;
  background: transparent;
}
.lib-reader-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
/* Lib reader actions — sl-icon-button + sl-button.lib-dl-btn */
sl-icon-button.lib-danger { --sl-color-neutral-700: var(--error); }
.lib-dl-wrap { position: relative; }
.lib-dl-menu {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  min-width: 180px;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  z-index: 10;
}
.lib-dl-menu.show { display: block; }
.lib-dl-menu a {
  display: block;
  padding: 8px 12px;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}
.lib-dl-menu a:hover { background: var(--bot-bg); }

.lib-reader-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
  padding: 0 4px 20px;
  line-height: 1.6;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.lib-reader-content h1, .lib-reader-content h2, .lib-reader-content h3 {
  margin-top: 1.2em;
  margin-bottom: 0.5em;
}
.lib-reader-content h1 { font-size: 1.6em; }
.lib-reader-content h2 { font-size: 1.3em; }
.lib-reader-content h3 { font-size: 1.1em; }
.lib-reader-content p { margin: 0.7em 0; }
.lib-reader-content ul, .lib-reader-content ol { padding-left: 1.5em; margin: 0.5em 0; }
.lib-reader-content code {
  background: var(--bot-bg);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.9em;
}
.lib-reader-content blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  margin: 1em 0;
  color: var(--muted);
}
.lib-reader-content table {
  display: block;
  overflow-x: auto;
  max-width: 100%;
  width: max-content;
  border-collapse: collapse;
  margin: 1em 0;
  -webkit-overflow-scrolling: touch;
}
.lib-reader-content th, .lib-reader-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
  white-space: normal;
  word-wrap: break-word;
}
.lib-reader-content pre {
  background: var(--bot-bg);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

/* iOS-style toggle switch */
.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 30px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border);
  transition: .25s;
  border-radius: 30px;
}
.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 2px;
  bottom: 2px;
  background: #fff;
  transition: .25s;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
input:checked + .slider { background: #34c759; }
input:checked + .slider:before { transform: translateX(20px); }
input:disabled + .slider { opacity: .6; cursor: default; }

/* Push modal specifics */
.push-desc {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}
.push-toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  background: var(--bg);
  border-radius: 10px;
}
.push-status { font-weight: 500; font-size: 15px; }
.push-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.push-msg {
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  min-height: 0;
}
.push-msg:empty { display: none; }
.push-msg.ok { background: rgba(52,199,89,.12); color: #1c8836; }
.push-msg.error { background: var(--error-bg); color: var(--error); }

.push-section {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.push-section h3 {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .5px;
}
.push-section input, .push-section textarea {
  width: 100%;
  margin-bottom: 8px;
}
.push-section textarea { resize: vertical; min-height: 60px; }

.push-history {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.hist-empty {
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 16px;
}
.hist-item {
  background: var(--bg);
  border-radius: 10px;
  border: 1px solid var(--border);
  cursor: pointer;
  padding: 14px 16px;
  transition: background .15s;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  box-sizing: border-box;
}
.hist-item:hover { background: var(--bot-bg); }
.hist-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}
.hist-title {
  font-weight: 600;
  font-size: 16px;
  color: var(--text);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.hist-meta {
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}
.hist-body {
  font-size: 15px;
  color: var(--text);
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.hist-item.expanded .hist-body {
  display: block;
  -webkit-line-clamp: unset;
  overflow: visible;
}
.hist-link {
  display: none;
  align-self: flex-start;
  margin-top: 4px;
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
}
.hist-item.expanded .hist-link { display: inline-block; }
.hist-link:hover { background: var(--accent-hover); }
.push-more-btn {
  width: 100%;
  margin-top: 8px;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
  font-size: 13px;
  padding: 8px 14px;
}

/* Install instructions modal */
.install-steps {
  padding-left: 22px;
  margin: 0 0 12px;
}
.install-steps li {
  margin-bottom: 10px;
  line-height: 1.5;
}
.install-hint {
  font-size: 13px;
  color: var(--muted);
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 8px;
  margin: 0;
}
.ios-share {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  padding: 1px 8px;
  border-radius: 4px;
  font-weight: 600;
  margin: 0 2px;
}
.install-go-btn {
  width: 100%;
  margin-top: 8px;
  padding: 12px 18px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 12px;
}

/* .lib-btn / .stats-btn — теперь sl-button.sidebar-btn (см. выше) */

/* Usage bars */
.usage-bars {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}
.usage-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
}
.usage-row .u-ico { flex: 0 0 14px; font-size: 12px; opacity: .8; }
.usage-row .u-bar {
  flex: 1;
  height: 5px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.usage-row .u-fill {
  height: 100%;
  border-radius: 3px;
  transition: width .3s, background-color .3s;
}
.usage-row .u-fill.ok { background: #34c759; }
.usage-row .u-fill.warn { background: #ff9f0a; }
.usage-row .u-fill.danger { background: #ff453a; }
.usage-row .u-lbl {
  flex: 0 0 auto;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  min-width: 48px;
  text-align: right;
}

/* ── Stats page ── */

/* Stats page (admin) — стили целиком теперь в stats.html inline. Здесь только
   global rules чтобы snippet'ы stats-body не падали при отсутствии inline стилей. */
.stats-body { overflow: auto; height: auto; }

.stats-empty {
  text-align: center;
  color: var(--muted);
  padding: 20px;
}

.badge {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  margin-left: 6px;
  vertical-align: middle;
}
