/* ── Тема: светлая / тёмная (монохром, как в ChatGPT) ── */
html[data-theme="light"]{
  --bg:#ffffff; --surface:#ffffff; --surface-2:#f4f4f4; --surface-3:#ececec;
  --text:#0d0d0d; --muted:#8e8ea0; --border:#e6e6e6; --border-2:#dcdcdc;
  --user-bubble:#f4f4f4; --send-bg:#0d0d0d; --send-fg:#ffffff;
  --shadow:0 12px 40px -12px rgba(0,0,0,.22);
  --scrim:rgba(0,0,0,.32); --danger:#e5484d;
}
html[data-theme="dark"]{
  --bg:#212121; --surface:#171717; --surface-2:#2f2f2f; --surface-3:#404040;
  --text:#ececec; --muted:#9a9aa5; --border:#3a3a3a; --border-2:#454545;
  --user-bubble:#2f2f2f; --send-bg:#ffffff; --send-fg:#0d0d0d;
  --shadow:0 12px 40px -12px rgba(0,0,0,.6);
  --scrim:rgba(0,0,0,.55); --danger:#ff6b6b;
}
:root{ --radius:16px; }

*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{
  font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
  background:var(--bg);color:var(--text);font-size:15px;line-height:1.55;
  -webkit-tap-highlight-color:transparent;
  transition:background .2s ease, color .2s ease;
}
.muted{color:var(--muted)}
.hidden{display:none !important}
button{font-family:inherit}

#app{display:flex;height:100vh;height:100dvh;overflow:hidden;position:relative}

/* ── Затемнение под шторкой ── */
.scrim{
  position:fixed;inset:0;background:var(--scrim);z-index:24;
  opacity:0;transition:opacity .25s ease;
}
.scrim:not(.hidden){opacity:1}

/* ── Сайдбар / шторка ── */
#sidebar{
  width:300px;max-width:86vw;flex-shrink:0;background:var(--surface);
  display:flex;flex-direction:column;overflow:hidden;position:relative;
  border-right:1px solid var(--border);
  transition:transform .28s cubic-bezier(.4,0,.2,1);
  padding-top:max(8px, env(safe-area-inset-top,0px) + var(--tg-safe-top,0px));
}
.sb-head{
  display:flex;align-items:center;justify-content:space-between;
  padding:14px 16px 8px;
}
.sb-title{font-size:24px;font-weight:700;letter-spacing:-.01em}
.sb-head-actions{display:flex;gap:6px;background:var(--surface-2);border-radius:999px;padding:4px}
.pill-icon{
  width:38px;height:38px;border:none;background:transparent;color:var(--text);
  border-radius:999px;display:flex;align-items:center;justify-content:center;cursor:pointer;
  transition:background .15s;
}
.pill-icon:active{background:var(--surface-3)}
.chat-search{
  margin:4px 14px 6px;padding:10px 14px;background:var(--surface-2);color:var(--text);
  border:none;border-radius:12px;font-size:14px;outline:none;
}
.sb-menu{padding:6px 8px}
.sb-menu-item{
  width:100%;display:flex;align-items:center;gap:14px;padding:11px 12px;border:none;
  background:transparent;color:var(--text);font-size:16px;cursor:pointer;border-radius:12px;text-align:left;
}
.sb-menu-item:active{background:var(--surface-2)}
.sb-section{padding:14px 18px 6px;font-size:14px;font-weight:600;color:var(--muted)}
.sb-subhead{display:flex;justify-content:space-between;padding:12px 14px 6px}
.text-btn{background:none;border:none;color:var(--text);font-size:14px;cursor:pointer;padding:6px 8px;border-radius:8px}
.text-btn:active{background:var(--surface-2)}

.session-list{flex:1;overflow-y:auto;padding:2px 8px 96px;-webkit-overflow-scrolling:touch}
.session-item{
  display:flex;align-items:center;gap:8px;padding:11px 12px;border-radius:12px;
  cursor:pointer;color:var(--text);font-size:15px;position:relative;
}
.session-item:active{background:var(--surface-2)}
.session-item.active{background:var(--surface-2)}
.session-item .name,.session-item .stitle{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.session-item .acts{display:flex;gap:2px;opacity:0;transition:opacity .15s}
.session-item:hover .acts,.session-item:active .acts{opacity:1}
.session-item .acts button{
  background:none;border:none;color:var(--muted);cursor:pointer;font-size:14px;padding:4px 6px;border-radius:8px;
}
.session-item .acts button:active{background:var(--surface-3)}
.projects-panel{display:flex;flex-direction:column;flex:1;overflow:hidden}

.stats-bar{display:none}

/* плавающая кнопка нового чата */
.fab-newchat{
  position:absolute;right:16px;bottom:max(16px, env(safe-area-inset-bottom,0px) + var(--tg-safe-bottom,0px) + 8px);
  z-index:5;display:flex;align-items:center;gap:8px;
  background:var(--send-bg);color:var(--send-fg);border:none;border-radius:999px;
  padding:13px 20px;font-size:15px;font-weight:600;cursor:pointer;
  box-shadow:0 6px 20px -6px rgba(0,0,0,.45);
}
.fab-newchat:active{transform:scale(.97)}

/* ── Основная область ── */
#main{flex:1;display:flex;flex-direction:column;min-width:0;background:var(--bg)}
#topbar{
  flex-shrink:0;display:flex;align-items:center;gap:10px;
  padding:max(8px, env(safe-area-inset-top,0px) + var(--tg-safe-top,0px)) 12px 8px;
  min-height:54px;
}
.round-btn{
  width:40px;height:40px;flex-shrink:0;border:none;background:var(--surface-2);color:var(--text);
  border-radius:999px;display:flex;align-items:center;justify-content:center;cursor:pointer;
  transition:background .15s,transform .1s;
}
.round-btn:active{background:var(--surface-3);transform:scale(.94)}
#currentTitle{flex:1;font-weight:600;font-size:16px;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding:0 4px}
.topbar-right{display:flex;align-items:center;gap:8px}

/* индикатор контекста */
.ctx{display:flex;align-items:center;gap:6px;font-size:11px;color:var(--muted)}
.ctx.hidden{display:none}
.ctx-bar{width:46px;height:6px;border-radius:4px;background:var(--surface-3);overflow:hidden}
.ctx-fill{height:100%;width:0;background:#10a37f;transition:width .4s,background .4s}
.ctx.high .ctx-fill{background:var(--danger)}
.ctx-text{font-variant-numeric:tabular-nums;min-width:42px}

/* ── Лента сообщений ── */
.messages{flex:1;overflow-y:auto;padding:8px 0 12px;scroll-behavior:smooth;-webkit-overflow-scrolling:touch}
.welcome{max-width:720px;margin:0 auto;min-height:100%;display:flex;flex-direction:column;justify-content:flex-end;padding:0 16px 8px}
.welcome h1{font-size:28px;margin:0 0 6px;font-weight:700}
.quick-actions{display:flex;flex-direction:column;gap:2px;margin-bottom:6px}
.qa{
  display:flex;align-items:center;gap:16px;padding:14px 10px;border:none;background:transparent;
  color:var(--text);font-size:16px;cursor:pointer;border-radius:14px;text-align:left;
}
.qa svg{flex-shrink:0;color:var(--text)}
.qa:active{background:var(--surface-2)}

.msg{max-width:780px;margin:0 auto;padding:10px 16px;display:flex;gap:12px}
.msg .avatar{width:30px;height:30px;border-radius:50%;flex-shrink:0;overflow:hidden}
.msg .avatar img{width:100%;height:100%;object-fit:cover;border-radius:50%}
.msg .body{flex:1;min-width:0;padding-top:2px}

/* пользовательские — пузырь справа */
.msg.user{justify-content:flex-end}
.msg.user .body{
  flex:0 1 auto;max-width:82%;padding:10px 15px;border-radius:20px 20px 6px 20px;
  background:var(--user-bubble);color:var(--text);white-space:pre-wrap;word-wrap:break-word;
}
.msg.user .body .files{margin:0 0 6px}

.msg.system{justify-content:center}
.msg.system .body{max-width:600px}
.sysbox{background:var(--surface-2);border-radius:14px;padding:12px 16px;font-size:13.5px;color:var(--muted)}
.sysbox b{color:var(--text)}
.sysbox .cmd{color:var(--text);font-family:Menlo,monospace}
.help-list{margin-top:8px;display:flex;flex-direction:column;gap:5px}

/* контент ответа */
.answer{word-wrap:break-word;overflow-wrap:anywhere;font-size:16px;color:var(--text)}
.answer p{margin:0 0 12px}
.answer pre{
  background:var(--surface-2);border:1px solid var(--border);border-radius:12px;
  padding:14px;overflow-x:auto;position:relative;margin:10px 0;
}
.answer code{font-family:"SF Mono",Menlo,Consolas,monospace;font-size:13.5px}
.answer :not(pre)>code{background:var(--surface-2);padding:2px 6px;border-radius:6px;font-size:13.5px}
.answer pre .copy-btn{
  position:absolute;top:8px;right:8px;background:var(--surface-3);border:none;
  color:var(--muted);font-size:11px;padding:4px 9px;border-radius:7px;cursor:pointer;opacity:0;
}
.answer pre:hover .copy-btn{opacity:1}
.answer table{border-collapse:collapse;margin:10px 0}
.answer th,.answer td{border:1px solid var(--border);padding:6px 10px}
.answer h1,.answer h2,.answer h3{margin:16px 0 8px}
.answer ul,.answer ol{margin:0 0 12px;padding-left:22px}
.answer blockquote{border-left:3px solid var(--border-2);margin:10px 0;padding:2px 14px;color:var(--muted)}

/* блоки работы инструментов */
.tool-block{
  border:1px solid var(--border);background:var(--surface-2);border-radius:12px;
  margin:8px 0;overflow:hidden;font-size:13px;
}
.tool-head{display:flex;align-items:center;gap:8px;padding:10px 12px;cursor:pointer;color:var(--muted);user-select:none}
.tool-head:active{background:var(--surface-3)}
.tool-head .chev{transition:transform .15s;font-size:10px}
.tool-block.open .tool-head .chev{transform:rotate(90deg)}
.tool-head .tname{font-weight:600;color:var(--text)}
.tool-head .tlabel{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-family:Menlo,monospace;font-size:12px}
.tool-head .tdur{font-size:11px;color:var(--muted)}
.tool-head .tdone{color:#10a37f;font-weight:700}
.tool-head .spinner{width:12px;height:12px;border:2px solid var(--border-2);border-top-color:var(--text);border-radius:50%;animation:spin .7s linear infinite}
@keyframes spin{to{transform:rotate(360deg)}}
.tool-body{display:none;padding:10px 12px;border-top:1px solid var(--border);background:var(--bg);max-height:340px;overflow:auto}
.tool-block.open .tool-body{display:block}
.tool-body pre{margin:0;white-space:pre-wrap;word-break:break-word;font-family:Menlo,monospace;font-size:12px;color:var(--text)}

.files{margin-top:10px;display:flex;flex-direction:column;gap:6px}
.file-link{display:inline-flex;align-items:center;gap:8px;background:var(--surface-2);border:1px solid var(--border);border-radius:10px;padding:9px 13px;color:var(--text);text-decoration:none;font-size:13.5px;width:fit-content}
.file-link:active{background:var(--surface-3)}

.typing{display:inline-flex;gap:4px;align-items:center;height:20px}
.typing span{width:7px;height:7px;border-radius:50%;background:var(--muted);animation:dots 1.4s infinite}
.typing span:nth-child(2){animation-delay:.2s}
.typing span:nth-child(3){animation-delay:.4s}
@keyframes dots{0%,60%,100%{opacity:.3;transform:scale(.8)}30%{opacity:1;transform:scale(1)}}

/* ── Композер ── */
#composer{
  padding:8px max(8px, env(safe-area-inset-right,0px)) max(10px, env(safe-area-inset-bottom,0px) + var(--tg-safe-bottom,0px)) max(8px, env(safe-area-inset-left,0px));
  background:var(--bg);position:relative;
}
.attachments{max-width:780px;margin:0 auto 8px;padding:0 8px;display:flex;flex-wrap:wrap;gap:8px}
.attachments:empty{display:none}
.attach-chip{display:flex;align-items:center;gap:8px;background:var(--surface-2);border:1px solid var(--border);border-radius:14px;padding:6px 10px;font-size:12px}
.attach-chip img{width:32px;height:32px;object-fit:cover;border-radius:8px}
.attach-chip .x{cursor:pointer;color:var(--muted);font-weight:700}

.input-shell{
  max-width:780px;margin:0 auto;padding:8px 8px 8px 16px;
  display:flex;flex-direction:column;gap:4px;
  background:var(--surface-2);border:1px solid var(--border);border-radius:26px;
  transition:border-color .15s;
}
.input-shell:focus-within{border-color:var(--border-2)}

/* синий чип-режим (как функция в ChatGPT) */
.mode-chip{
  display:inline-flex;align-items:center;gap:6px;align-self:flex-start;
  background:rgba(47,124,246,.14);color:#2f7cf6;
  border-radius:999px;padding:5px 6px 5px 12px;font-size:14px;font-weight:600;margin:2px 0;
}
.mode-x{
  border:none;background:transparent;color:#2f7cf6;cursor:pointer;font-size:13px;
  width:20px;height:20px;border-radius:50%;display:flex;align-items:center;justify-content:center;
}
.mode-x:active{background:rgba(47,124,246,.22)}
#app.mode-on .quick-actions{display:none}
#input{
  width:100%;resize:none;max-height:180px;min-height:24px;
  background:transparent;color:var(--text);border:none;padding:6px 4px 2px;
  font-size:16px;font-family:inherit;line-height:1.5;outline:none;
}
#input::placeholder{color:var(--muted)}
.input-row{display:flex;align-items:center;gap:6px;padding-right:2px}
.row-spacer{flex:1}
.icon-btn{
  width:38px;height:38px;border-radius:50%;border:none;background:transparent;color:var(--text);
  cursor:pointer;display:flex;align-items:center;justify-content:center;flex-shrink:0;
  transition:background .15s,transform .12s;
}
.icon-btn:active{transform:scale(.9)}
#attachBtn{background:var(--surface-3)}
.icon-btn.primary{background:var(--send-bg);color:var(--send-fg)}
.icon-btn.primary:disabled{cursor:default}

/* модель-чип */
.model-chip{
  display:flex;align-items:center;gap:4px;background:transparent;border:none;color:var(--text);
  font-size:15px;font-weight:500;cursor:pointer;padding:6px 8px;border-radius:10px;max-width:42vw;
}
.model-chip:active{background:var(--surface-3)}
.model-chip #modelChipName{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.model-chip .chev{color:var(--muted);flex-shrink:0}

/* поповер выбора модели */
.model-popover{
  position:absolute;left:max(12px,env(safe-area-inset-left,0px));bottom:calc(100% - 4px);
  z-index:20;min-width:240px;max-width:min(82vw,360px);max-height:60vh;overflow-y:auto;
  background:var(--surface);border:1px solid var(--border);border-radius:18px;
  box-shadow:var(--shadow);padding:6px;
  animation:popIn .16s ease;
}
@keyframes popIn{from{opacity:0;transform:translateY(8px) scale(.98)}to{opacity:1;transform:none}}
.mp-title{padding:8px 12px 4px;font-size:13px;color:var(--muted)}
.mp-item{
  display:flex;align-items:center;gap:10px;padding:12px;border-radius:12px;cursor:pointer;
  font-size:16px;color:var(--text);
}
.mp-item:active{background:var(--surface-2)}
.mp-item .mp-name{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.mp-item .mp-tick{color:var(--text);font-weight:700;opacity:0}
.mp-item.sel .mp-tick{opacity:1}
.mp-item.sel{font-weight:600}
.mp-divider{height:1px;background:var(--border);margin:4px 10px}

/* меню слэш-команд */
.slash-menu{
  max-width:780px;margin:0 auto 8px;padding:6px;background:var(--surface);
  border:1px solid var(--border);border-radius:16px;box-shadow:var(--shadow);
  max-height:300px;overflow-y:auto;
}
.slash-item{display:flex;align-items:baseline;gap:10px;padding:10px 12px;border-radius:10px;cursor:pointer}
.slash-item.sel,.slash-item:active{background:var(--surface-2)}
.slash-item .cmd{color:var(--text);font-weight:600;font-family:Menlo,monospace;font-size:13px;flex-shrink:0}
.slash-item .desc{color:var(--muted);font-size:13px}

/* кнопка отправки → стоп */
#sendBtn .stop-icon{display:none}
#composer.busy #sendBtn .send-icon{display:none}
#composer.busy #sendBtn .stop-icon{display:block}
#voiceBtn.recording{color:var(--danger);animation:pulse 1s infinite}
@keyframes pulse{0%,100%{opacity:1}50%{opacity:.4}}

.msg-meta{font-size:11px;margin-top:8px;opacity:.65}
.speak-btn{cursor:pointer;opacity:.7;transition:opacity .15s}
.speak-btn:active{opacity:1}
body.ws-disconnected #topbar::after{content:"Переподключение…";font-size:11px;color:var(--danger)}

/* ── Лист настроек (тема) ── */
.sheet{position:fixed;inset:0;z-index:60;display:flex;align-items:flex-end;justify-content:center;background:var(--scrim)}
.sheet-card{
  width:100%;max-width:480px;background:var(--surface);border-radius:20px 20px 0 0;
  padding:18px 16px max(20px, env(safe-area-inset-bottom,0px) + var(--tg-safe-bottom,0px));
  animation:sheetUp .25s ease;
}
@keyframes sheetUp{from{transform:translateY(100%)}to{transform:none}}
.sheet-title{font-size:18px;font-weight:700;margin:4px 4px 14px}
.theme-opts{display:flex;flex-direction:column;gap:4px}
.theme-opt{
  display:flex;align-items:center;justify-content:space-between;width:100%;
  padding:14px;border:none;background:var(--surface-2);color:var(--text);
  border-radius:12px;font-size:16px;cursor:pointer;
}
.theme-opt .tick{opacity:0;font-weight:700}
.theme-opt.sel .tick{opacity:1}
.sheet-action{
  width:100%;margin-top:8px;padding:14px;border:none;border-radius:12px;
  background:var(--surface-2);color:var(--text);font-size:16px;cursor:pointer;text-align:center;
}
.sheet-action.busy{opacity:.5;cursor:default}
.sheet-close{
  width:100%;margin-top:14px;padding:14px;border:none;border-radius:14px;
  background:var(--send-bg);color:var(--send-fg);font-size:16px;font-weight:600;cursor:pointer;
}

/* ── Логин ── */
.overlay{position:fixed;inset:0;background:var(--scrim);display:flex;align-items:center;justify-content:center;z-index:70}
.login-box{background:var(--surface);border:1px solid var(--border);border-radius:18px;padding:28px;width:320px;max-width:88vw;text-align:center}
.login-box h2{margin:0 0 18px}
.login-box input{width:100%;padding:12px 14px;border-radius:12px;border:1px solid var(--border);background:var(--surface-2);color:var(--text);font-size:16px;margin-bottom:12px;outline:none}
.login-box button{width:100%;padding:12px;border-radius:12px;border:none;background:var(--send-bg);color:var(--send-fg);font-weight:600;cursor:pointer;font-size:16px}
.login-error{color:var(--danger);font-size:13px;margin-top:10px;min-height:18px}

/* ── Десктоп: сайдбар закреплён, шторка только на узких ── */
@media(min-width:861px){
  #sidebar{position:relative;transform:none !important}
  #app.drawer-open .scrim,#scrim{display:none !important}
  #menuBtn{display:none}
}
@media(max-width:860px){
  #sidebar{position:fixed;top:0;left:0;bottom:0;z-index:25;transform:translateX(-100%);box-shadow:0 0 40px rgba(0,0,0,.3)}
  #app.drawer-open #sidebar{transform:translateX(0)}
}
