:root {
  --bg: #14161b; --bg2: #1a1d24; --bg3: #232731; --line: #2c313d;
  --text: #e8e5de; --muted: #9a9fab; --faint: #666b7a;
  --accent: #e0a85a; --accent2: #f2c785; --accent-bg: rgba(224, 168, 90, .13);
  --user: #263044; --danger: #e46e6e; --ok: #6cc28a;
  --story: Georgia, 'PT Serif', 'Noto Serif', serif;
  --ui: 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.45 var(--ui); overscroll-behavior: none; }
button, input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }
::-webkit-scrollbar { width: 8px; } ::-webkit-scrollbar-thumb { background: #343a48; border-radius: 8px; }

#app { display: grid; grid-template-columns: 280px minmax(0, 1fr) auto; height: 100dvh; }
#side { background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; position: relative; padding-top: var(--safe-t); }
#main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
#mem { width: 370px; background: var(--bg2); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; padding-top: var(--safe-t); }

/* базовые элементы */
.icon-btn { border: 0; background: none; width: 38px; height: 38px; border-radius: 10px; cursor: pointer; font-size: 18px; color: var(--muted); display: inline-grid; place-items: center; flex: none; }
.icon-btn:hover { background: var(--bg3); color: var(--text); }
.icon-btn.on { color: var(--accent); }
.btn { border: 1px solid var(--line); background: var(--bg3); padding: 9px 16px; border-radius: 11px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #4a526a; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #1d1408; font-weight: 600; }
.btn.danger { color: #ffb0b0; border-color: rgba(228, 110, 110, .45); background: rgba(228, 110, 110, .12); }
.btn.ghost { background: none; border-color: transparent; color: var(--muted); }
.btn.sm { padding: 6px 11px; font-size: 13.5px; border-radius: 9px; }
.btn:disabled { opacity: .5; }
.only-m { display: none; }
.seg { display: flex; gap: 4px; padding: 8px 12px; }
.seg button { flex: 1; border: 0; background: none; color: var(--muted); padding: 7px 10px; border-radius: 9px; cursor: pointer; white-space: nowrap; }
.seg button.on { background: var(--accent-bg); color: var(--accent2); }
.seg.scroll { overflow-x: auto; scrollbar-width: none; border-bottom: 1px solid var(--line); }
.seg.scroll::-webkit-scrollbar { display: none; }
.seg.scroll button { flex: none; }
input.in, textarea.in, select.in { width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; outline: none; font-size: 16px; }
textarea.in { resize: vertical; line-height: 1.5; min-height: 80px; }
.in:focus { border-color: var(--accent); }
label.lbl { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 5px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 120px; }
.row.end { justify-content: flex-end; } .row.end > * { flex: none; min-width: 0; }
.switch { display: flex; align-items: center; gap: 10px; padding: 10px 0; cursor: pointer; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; }
.muted { color: var(--muted); font-size: 13px; }
details.more { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 6px; }
details.more summary { cursor: pointer; color: var(--muted); padding: 6px 0; }

/* боковая панель */
.side-top { display: flex; align-items: center; padding: 12px 10px 4px 16px; }
.brand { flex: 1; font-family: var(--story); font-size: 20px; color: var(--accent2); }
.list { flex: 1; overflow-y: auto; padding: 4px 8px 90px; }
.item { display: flex; gap: 11px; align-items: center; padding: 9px 10px; border-radius: 12px; cursor: pointer; }
.item:hover { background: var(--bg3); } .item.on { background: var(--accent-bg); }
.item-b { flex: 1; min-width: 0; }
.item-t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-s { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { color: var(--faint); text-align: center; padding: 30px 16px; }
.fab { position: absolute; right: 16px; bottom: calc(16px + var(--safe-b)); width: 54px; height: 54px; border-radius: 50%; border: 0; background: var(--accent); color: #1d1408; font-size: 26px; cursor: pointer; box-shadow: 0 6px 20px rgba(0, 0, 0, .4); }

.ava { width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center; font-weight: 700; color: #1d1408; overflow: hidden; background: #3a3f4d; }
.ava img { width: 100%; height: 100%; object-fit: cover; }
.ava.s { width: 34px; height: 34px; font-size: 14px; } .ava.l { width: 88px; height: 88px; font-size: 34px; }

/* домашний экран */
.home { margin: auto; text-align: center; padding: 30px 20px; max-width: 420px; }
.home h1 { font-family: var(--story); font-weight: 400; color: var(--accent2); font-size: 30px; margin: 0 0 18px; }
.home .btn { display: block; width: 100%; margin-top: 10px; padding: 13px; font-size: 16px; }

/* чат */
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.bar { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); min-height: 58px; }
.bar-title { flex: 1; min-width: 0; }
.t1 { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t2 { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 0 8px; overscroll-behavior: contain; }
.msg { max-width: 820px; margin: 0 auto; padding: 8px 20px; display: flex; gap: 12px; }
.msg-m { flex: 1; min-width: 0; }
.msg-n { font-size: 13.5px; font-weight: 600; color: var(--accent2); }
.msg.user .msg-n { color: #9fb6e6; }
.msg-x { font-family: var(--story); font-size: 17px; line-height: 1.7; margin-top: 2px; overflow-wrap: anywhere; }
.msg-x p { margin: 0 0 .7em; } .msg-x p:last-child { margin: 0; }
.msg-x em { color: #bdb7aa; }
.msg-x .sp { color: #f4ebd8; }
.msg.user .msg-m { background: var(--user); border-radius: 16px; padding: 9px 14px; }
.msg.user { flex-direction: row-reverse; } .msg.user .msg-n { text-align: right; }
.tools { display: flex; gap: 0; align-items: center; margin-top: 4px; visibility: hidden; flex-wrap: wrap; }
.msg:hover .tools, .msg.last .tools, .msg.show .tools { visibility: visible; }
.tools .icon-btn { width: 34px; height: 32px; font-size: 15px; }
.swipe { font-size: 12.5px; color: var(--muted); display: inline-flex; align-items: center; }
.msg.streaming .msg-x::after { content: '▍'; color: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.msg.flash .msg-m { animation: flash 1.6s; } @keyframes flash { 0%, 40% { box-shadow: 0 0 0 2px var(--accent); } }
.err { color: #ffbcbc; background: rgba(228, 110, 110, .1); border: 1px solid rgba(228, 110, 110, .35); border-radius: 12px; padding: 10px 14px; font-size: 14px; display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.edit textarea { width: 100%; min-height: 140px; }
.suggest { display: flex; gap: 8px; overflow-x: auto; padding: 6px 12px; max-width: 844px; margin: 0 auto; width: 100%; }
.chip-s { flex: none; max-width: 300px; border: 1px solid var(--line); background: var(--bg2); border-radius: 12px; padding: 8px 12px; font-size: 14px; cursor: pointer; text-align: left; white-space: normal; }
.chip-s:hover { border-color: var(--accent); }
.composer { display: flex; gap: 6px; align-items: flex-end; max-width: 844px; width: 100%; margin: 0 auto; padding: 8px 12px calc(10px + var(--safe-b)); }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); background: var(--bg2); border-radius: 20px; padding: 9px 14px; outline: none; font: 16px/1.45 var(--story); max-height: 40vh; }
.composer textarea:focus { border-color: #5d5240; }
.send { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: #1d1408; font-size: 18px; cursor: pointer; flex: none; }
.send.stop { background: var(--danger); color: #fff; }

/* память */
.mem-body { flex: 1; overflow-y: auto; padding: 12px; padding-bottom: calc(20px + var(--safe-b)); }
.card { background: var(--bg3); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.card.pin { border-color: rgba(224, 168, 90, .55); } .card.off { opacity: .45; }
.card-h { display: flex; align-items: center; gap: 4px; }
.card-n { flex: 1; font-weight: 600; min-width: 0; }
.card-n small { color: var(--faint); font-weight: 400; }
.card-h .icon-btn { width: 30px; height: 30px; font-size: 14px; }
.card-x { font-size: 14px; color: #d3cfc6; margin-top: 3px; white-space: pre-wrap; }
.card ul { margin: 5px 0 0; padding-left: 18px; color: var(--muted); font-size: 13.5px; }
.tag { font-size: 11.5px; border-radius: 6px; padding: 1px 7px; background: #323848; color: var(--muted); }
.tag.st { background: rgba(159, 182, 230, .14); color: #bccbee; display: inline-block; margin-top: 5px; }
.stars { color: var(--accent); font-size: 11px; }
.tools-top { display: flex; gap: 6px; margin-bottom: 10px; }
.tools-top .in { padding: 8px 12px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); display: inline-block; flex: none; }
.dot.work { background: var(--accent); animation: pulse 1s infinite; } .dot.err { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

/* окна */
.mwrap { position: fixed; inset: 0; z-index: 50; background: rgba(4, 5, 8, .6); display: grid; place-items: center; padding: 16px; }
.modal { background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; width: min(560px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0, 0, 0, .5); }
.modal.wide { width: min(860px, 100%); }
.mh { display: flex; align-items: center; padding: 12px 10px 8px 20px; }
.mh h2 { flex: 1; margin: 0; font-size: 18px; font-weight: 600; }
.mb { padding: 4px 20px 16px; overflow-y: auto; }
.mf { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 20px calc(16px + var(--safe-b)); border-top: 1px solid var(--line); flex-wrap: wrap; }
.menu { display: grid; gap: 4px; }
.menu button { text-align: left; border: 0; background: none; padding: 12px 10px; border-radius: 10px; cursor: pointer; font-size: 15.5px; }
.menu button:hover { background: var(--bg3); }
.picks { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.pick { border: 1px solid var(--line); border-radius: 14px; padding: 12px 8px; text-align: center; cursor: pointer; background: var(--bg3); }
.pick.on { border-color: var(--accent); background: var(--accent-bg); }
.pick .ava { margin: 0 auto 6px; width: 54px; height: 54px; }
.pick > div:last-child { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pick { position: relative; }
.pick .pe { position: absolute; top: 2px; right: 2px; width: 28px; height: 28px; font-size: 13px; }
.pick .ico { width: 54px; height: 54px; margin: 0 auto 6px; display: grid; place-items: center; font-size: 26px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button { border: 1px solid var(--line); background: var(--bg3); border-radius: 20px; padding: 7px 13px; cursor: pointer; font-size: 14px; }
.chips button.on { border-color: var(--accent); background: var(--accent-bg); color: var(--accent2); }
.step-t { font-weight: 600; margin: 14px 0 8px; }
.step-t:first-child { margin-top: 0; }
pre.ctx { white-space: pre-wrap; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px; font: 12.5px/1.5 Consolas, monospace; max-height: 320px; overflow: auto; }
.lore { border: 1px solid var(--line); border-radius: 10px; padding: 8px; margin-top: 8px; display: grid; gap: 6px; }
.res { padding: 10px; border-radius: 10px; cursor: pointer; } .res:hover { background: var(--bg3); }
.pin-screen { margin: auto; text-align: center; max-width: 300px; padding: 30px 20px; }
.pin-screen input { text-align: center; font-size: 26px; letter-spacing: 8px; margin: 16px 0; }
.big { font-size: 22px; font-weight: 700; color: var(--accent2); letter-spacing: 3px; }

#toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + var(--safe-b)); z-index: 90; display: grid; gap: 8px; width: max-content; max-width: 92vw; }
.toast { background: #2c313d; border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; font-size: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, .4); }
.toast.e { border-color: rgba(228, 110, 110, .6); color: #ffc7c7; }

@media (max-width: 1100px) {
  #mem { position: fixed; inset: 0 0 0 auto; z-index: 30; width: min(400px, 100%); box-shadow: -10px 0 40px rgba(0, 0, 0, .5); }
}
/* телефон: экраны как в мессенджере */
@media (max-width: 760px) {
  #app { grid-template-columns: minmax(0, 1fr); }
  #side { min-width: 0; }
  #side { border: 0; }
  body.in-chat #side { display: none; }
  body:not(.in-chat) #main { display: none; }
  #mem { width: 100%; }
  .only-m { display: inline-grid; }
  .msg { padding: 6px 12px; }
  .msg .ava { display: none; }
  .msg-x { font-size: 16.5px; }
  .tools { visibility: visible; display: none; }
  .msg.show .tools, .msg.last .tools { display: flex; }
  .mwrap { padding: 0; place-items: end stretch; }
  .modal, .modal.wide { width: 100%; max-height: 92dvh; border-radius: 18px 18px 0 0; }
  .icon-btn { width: 42px; height: 42px; }
}
