:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --sec: var(--tg-theme-secondary-bg-color, #232e3c);
  --text: var(--tg-theme-text-color, #f5f5f5);
  --hint: var(--tg-theme-hint-color, #7d8b99);
  --link: var(--tg-theme-link-color, #6ab3f3);
  --btn: var(--tg-theme-button-color, #5288c1);
  --btn-text: var(--tg-theme-button-text-color, #fff);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; height: 100vh; overflow: hidden;
}
#appbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; background: var(--sec); flex: 0 0 auto;
}
.tabs { display: flex; gap: 6px; }
.tab {
  background: transparent; border: 0; color: var(--hint);
  font-size: 15px; padding: 6px 10px; border-radius: 8px; cursor: pointer;
}
.tab.active { background: var(--btn); color: var(--btn-text); }
.ver { color: var(--hint); font-size: 11px; }

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

/* чат */
.messages { flex: 1 1 auto; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 85%; padding: 8px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.me { align-self: flex-end; background: var(--btn); color: var(--btn-text); border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--sec); border-bottom-left-radius: 4px; }
.msg.pending { opacity: .6; font-style: italic; }
.composer { display: flex; gap: 8px; padding: 10px 12px; background: var(--sec); flex: 0 0 auto; }
.composer textarea {
  flex: 1; resize: none; border: 0; border-radius: 18px; padding: 9px 14px;
  background: var(--bg); color: var(--text); font-size: 15px; max-height: 120px;
}
.composer button {
  border: 0; border-radius: 50%; width: 40px; height: 40px;
  background: var(--btn); color: var(--btn-text); font-size: 18px; cursor: pointer;
}
.composer button:disabled { opacity: .5; }

/* статус */
.cards { display: flex; gap: 8px; padding: 12px; flex: 0 0 auto; }
.card { flex: 1; background: var(--sec); border-radius: 12px; padding: 12px; text-align: center;
  display: flex; flex-direction: column; justify-content: space-between; }
.card-t { color: var(--hint); font-size: 12px; min-height: 2.4em;
  display: flex; align-items: center; justify-content: center; }
.card-v { font-size: 26px; font-weight: 600; margin-top: 6px; }
.list { flex: 1 1 auto; overflow-y: auto; padding: 0 12px; }
.list .item { background: var(--sec); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.list .item .due { color: var(--hint); font-size: 12px; }
.refresh {
  margin: 12px; border: 0; border-radius: 10px; padding: 12px;
  background: var(--btn); color: var(--btn-text); font-size: 15px; cursor: pointer; flex: 0 0 auto;
}

/* настройки (LLM-роутер) */
.settings { padding: 14px; overflow-y: auto; }
.set-block { margin-bottom: 20px; }
.set-label { font-size: 14px; margin-bottom: 8px; }
.set-hint { color: var(--hint); font-size: 12px; margin-top: 6px; }
.seg { display: flex; gap: 6px; }
.seg-btn { flex: 1; padding: 10px 0; border: 0; border-radius: 10px;
  background: var(--sec); color: var(--text); font-size: 14px; cursor: pointer; }
.seg-btn.active { background: var(--btn); color: var(--btn-text); font-weight: 600; }
.settings input[type=range] { width: 100%; }
.set-saved { color: var(--hint); font-size: 12px; margin-top: 10px; text-align: center; min-height: 1em; }

/* время сообщений (#107) */
.msg-time { font-size: 10px; color: var(--hint); margin: -4px 6px 8px; line-height: 1.2; }
.msg-time.me { text-align: right; }
.msg-time.bot { text-align: left; }
.msg-time:empty { display: none; }

/* карточки Q&A + комментарии (#161/166): каждое «вопрос→ответ» отдельным блоком */
.qa-card {
  display: flex; flex-direction: column; gap: 8px;
  background: rgba(255,255,255,.03); border: 1px solid rgba(255,255,255,.05);
  border-radius: 14px; padding: 10px;
}
.qa-card .msg { max-width: 100%; }
.qa-card.flash { animation: qaflash 1.5s ease-out; }
@keyframes qaflash {
  0% { background: rgba(106,179,243,.28); border-color: var(--btn); }
  100% { background: rgba(255,255,255,.03); border-color: rgba(255,255,255,.05); }
}
.qa-actions { margin-top: 2px; }
.qa-c-add {
  border: 0; cursor: pointer; background: transparent; color: var(--hint);
  font-size: 13px; padding: 4px 8px; border-radius: 8px;
}
.qa-c-add:hover, .qa-c-add:active { color: var(--link); }

/* блок-комментарий со ссылкой на ответ (#171) */
.cmt-card {
  align-self: flex-start; max-width: 92%;
  background: var(--sec); border-left: 3px solid var(--btn);
  border-radius: 12px; padding: 8px 12px; display: flex; flex-direction: column; gap: 4px;
}
.cmt-ref {
  font-size: 12px; color: var(--link); cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cmt-ref:hover { text-decoration: underline; }
.cmt-body { font-size: 14px; white-space: pre-wrap; word-wrap: break-word; }
.cmt-editor {
  align-self: stretch; background: var(--sec); border: 1px solid var(--btn);
  border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 6px;
}
.cmt-editor .cmt-ref { cursor: default; color: var(--hint); }
/* подсветка коммента на месте при добавлении (без авто-скролла) */
.cmt-card.flash { animation: qaflash 1.5s ease-out; }

/* Календарь (#187) */
.cal-root { flex: 1 1 auto; overflow-y: auto; padding: 10px; }
.cal-hdr { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.cal-nav { border: 0; background: var(--sec); color: var(--text); border-radius: 10px;
  font-size: 18px; padding: 6px 12px; cursor: pointer; }
.cal-title { flex: 1; border: 0; background: transparent; color: var(--text);
  font-size: 17px; font-weight: 600; cursor: pointer; text-align: center; }
/* разлиновка месяца: тонкие линии-сетка через 1px gap на фоне-цвете линий */
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px;
  background: var(--hint); border: 1px solid var(--hint);
  border-radius: 10px; overflow: hidden; }
.cal-wd { margin-bottom: 6px; gap: 2px; background: transparent; border: 0; border-radius: 0; }
.cal-wdh { text-align: center; color: var(--hint); font-size: 12px; padding: 2px 0; }
.cal-cell { position: relative; aspect-ratio: 1 / 1; border: 0; border-radius: 0;
  background: var(--sec); color: var(--text); cursor: pointer; display: flex;
  align-items: flex-start; justify-content: flex-start; padding: 6px; font-size: 14px; }
.cal-cell.empty { background: rgba(255,255,255,.02); cursor: default; }
.cal-cell.today { outline: 2px solid var(--btn); }
.cal-cell.has-ev { background: rgba(82,136,193,.22); }
.cal-d { line-height: 1; }
.cal-dot { position: absolute; right: 5px; bottom: 5px; min-width: 16px; height: 16px;
  border-radius: 8px; background: var(--btn); color: var(--btn-text);
  font-size: 11px; line-height: 16px; text-align: center; padding: 0 3px; }
/* год — разлиновка 3×4 */
.cal-year { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px;
  background: var(--hint); border: 1px solid var(--hint);
  border-radius: 12px; overflow: hidden; }
.cal-mcell { position: relative; aspect-ratio: 3 / 2; border: 0; border-radius: 0;
  background: var(--sec); color: var(--text); cursor: pointer; font-size: 15px;
  display: flex; align-items: center; justify-content: center; }
.cal-mcell.has-ev { background: rgba(82,136,193,.22); }
.cal-mbadge { position: absolute; right: 8px; top: 8px; min-width: 18px; height: 18px;
  border-radius: 9px; background: var(--btn); color: var(--btn-text);
  font-size: 11px; line-height: 18px; text-align: center; padding: 0 4px; }
/* день — 24 часа, разлиновка «тетрадь в линеечку»: чёткая линия на КАЖДЫЙ час (#192).
   Цвет — var(--hint) (виден и на тёмной, и на светлой теме, в отличие от белого с альфой). */
.cal-day { display: flex; flex-direction: column; gap: 0;
  border: 1px solid var(--hint); border-radius: 10px; overflow: hidden; }
.cal-hour { display: flex; align-items: stretch; gap: 8px; min-height: 38px;
  border-top: 1px solid var(--hint); padding: 3px 6px; }
.cal-hour:first-child { border-top: 0; }
.cal-hlabel { flex: 0 0 48px; color: var(--hint); font-size: 12px; padding: 6px 8px 0 0;
  border-right: 1px solid var(--hint); }
.cal-hbody { flex: 1 1 auto; display: flex; flex-direction: column; gap: 4px; justify-content: center; }
.cal-hadd { flex: 0 0 32px; border: 0; border-radius: 8px; background: var(--sec);
  color: var(--hint); font-size: 16px; cursor: pointer; }
.cal-hadd:hover { color: var(--btn-text); background: var(--btn); }
.cal-ev { display: flex; align-items: center; gap: 8px; background: var(--btn);
  color: var(--btn-text); border-radius: 8px; padding: 5px 8px; font-size: 13px; }
.cal-evt { font-variant-numeric: tabular-nums; opacity: .85; flex: 0 0 auto; }
.cal-evtitle { flex: 1 1 auto; word-break: break-word; }
.cal-evdel { border: 0; background: transparent; cursor: pointer; font-size: 13px; flex: 0 0 auto; }
.cal-addbox { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 10px;
  background: var(--sec); border: 1px solid var(--btn); border-radius: 12px; padding: 8px; }
.cal-arem { flex: 0 0 auto; border: 0; border-radius: 8px; padding: 8px; background: var(--bg); color: var(--text); font-size: 13px; }
.cal-atime { flex: 0 0 auto; border: 0; border-radius: 8px; padding: 7px; background: var(--bg); color: var(--text); }
.cal-atitle { flex: 1 1 auto; border: 0; border-radius: 8px; padding: 8px 10px; background: var(--bg); color: var(--text); font-size: 14px; }
.cal-aok { border: 0; border-radius: 8px; padding: 8px 12px; background: var(--btn); color: var(--btn-text); cursor: pointer; }
.cal-acancel { border: 0; border-radius: 8px; padding: 8px 10px; background: transparent; color: var(--hint); cursor: pointer; }

.qa-comment-input {
  width: 100%; resize: vertical; min-height: 40px; border: 0; border-radius: 10px;
  padding: 8px 10px; background: var(--bg); color: var(--text); font-size: 14px;
}
.qa-comment-bar { display: flex; gap: 8px; margin-top: 6px; }
.qa-comment-bar button {
  border: 0; cursor: pointer; border-radius: 8px; padding: 7px 14px; font-size: 13px;
}
.qa-c-save { background: var(--btn); color: var(--btn-text); }
.qa-c-cancel { background: var(--sec); color: var(--text); }

/* прикрепление файлов (#174/180): чипсы вложений + вкладка Файлы */
.attach-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px;
  flex: 0 0 auto; max-height: 92px; overflow-y: auto; }
.attach-chips:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--sec);
  color: var(--text); font-size: 12px; padding: 5px 8px; border-radius: 12px; max-width: 100%; }
.chip-x { border: 0; background: transparent; color: var(--hint); cursor: pointer;
  font-size: 12px; padding: 0 2px; }
.chip-x:hover { color: var(--text); }
#attach { border: 0; border-radius: 50%; width: 40px; height: 40px; flex: 0 0 auto;
  background: var(--sec); color: var(--text); font-size: 18px; cursor: pointer; }
.file-item { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.file-dl { border: 0; cursor: pointer; border-radius: 8px; padding: 7px 12px; font-size: 13px;
  background: var(--btn); color: var(--btn-text); flex: 0 0 auto; }

/* контекстное меню выделенного текста (#164): копировать / сохранить / отправить */
.ctxmenu {
  position: fixed; z-index: 9999; min-width: 170px;
  background: var(--sec); border-radius: 10px; padding: 6px;
  border: 1px solid rgba(255,255,255,.07); box-shadow: 0 8px 30px rgba(0,0,0,.45);
}
.ctxmenu button {
  display: block; width: 100%; text-align: left; border: 0; cursor: pointer;
  background: transparent; color: var(--text); font-size: 14px;
  padding: 10px 12px; border-radius: 8px;
}
.ctxmenu button:hover, .ctxmenu button:active { background: var(--btn); color: var(--btn-text); }
.sel-pill {
  position: fixed; z-index: 9999; border: 0; cursor: pointer;
  background: var(--btn); color: var(--btn-text); font-size: 13px;
  padding: 6px 12px; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,.4);
}
.ctx-toast {
  position: fixed; left: 50%; bottom: 72px; transform: translateX(-50%) translateY(10px);
  z-index: 10000; background: var(--sec); color: var(--text);
  padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none;
  box-shadow: 0 6px 24px rgba(0,0,0,.45);
}
.ctx-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
