:root {
  --navy: #002653; --navy2: #1F2E50; --rust: #A8402F; --orange: #FD740F;
  --bg: #F4F6F9; --panel: #FFFFFF; --line: #DDE3EA; --soft: #E6EBF1;
  --text: #1A2233; --muted: #6B7689; --user: #E3EAF3;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif; color: var(--text); background: var(--bg);
  -webkit-text-size-adjust: 100%; overscroll-behavior: none; }
button { font: inherit; cursor: pointer; border: 0; background: none; color: inherit; }
.hidden { display: none !important; }

/* ---------- каркас ---------- */
.app { display: flex; height: 100dvh; }
.side { width: 280px; flex: none; background: var(--navy); color: #fff; display: flex; flex-direction: column;
  padding-top: var(--safe-t); }
.side .brand { padding: 16px 16px 12px; font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 10px; }
.side .brand i { width: 28px; height: 28px; border-radius: 7px; background: var(--orange); display: grid; place-items: center;
  font-style: normal; font-size: 13px; }
.side .new { margin: 0 12px 10px; padding: 10px 12px; border-radius: 10px; background: rgba(255,255,255,.12); text-align: left; }
.side .new:hover { background: rgba(255,255,255,.2); }
.tabs { display: flex; margin: 0 12px 8px; background: rgba(255,255,255,.08); border-radius: 9px; padding: 3px; }
.tabs button { flex: 1; padding: 6px; border-radius: 7px; color: #C9D3E3; font-size: 13px; }
.tabs button.on { background: rgba(255,255,255,.18); color: #fff; }
.threads { flex: 1; overflow-y: auto; padding: 0 8px; }
.threads a { display: block; padding: 9px 10px; border-radius: 8px; color: #D7DFEA; text-decoration: none; font-size: 14px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.threads a:hover { background: rgba(255,255,255,.08); }
.threads a.on { background: rgba(255,255,255,.16); color: #fff; }
.side .foot { padding: 12px 16px calc(12px + var(--safe-b)); border-top: 1px solid rgba(255,255,255,.12); font-size: 13px; color: #C9D3E3; }
.side .foot .bar { height: 5px; border-radius: 3px; background: rgba(255,255,255,.15); margin: 6px 0 10px; overflow: hidden; }
.side .foot .bar b { display: block; height: 100%; background: var(--orange); }
.side .foot button { color: #C9D3E3; text-decoration: underline; padding: 0; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.top { display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--safe-t)) 14px 10px; background: var(--panel);
  border-bottom: 1px solid var(--line); }
.top .menu { display: none; font-size: 22px; padding: 0 4px; color: var(--navy); }
.top .title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top .title input { width: 100%; font: inherit; border: 1px solid var(--line); border-radius: 6px; padding: 2px 6px; }
.seg { display: flex; background: var(--soft); border-radius: 9px; padding: 2px; font-size: 13px; }
.seg button { padding: 5px 10px; border-radius: 7px; color: var(--muted); }
.seg button.on { background: var(--panel); color: var(--navy); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* ---------- сообщения ---------- */
.feed { flex: 1; overflow-y: auto; padding: 18px 14px 8px; -webkit-overflow-scrolling: touch; }
.feed .inner { max-width: 860px; margin: 0 auto; }
.empty { text-align: center; color: var(--muted); margin-top: 12vh; }
.empty h2 { color: var(--navy); margin: 0 0 6px; }
.hints { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-top: 22px; text-align: left; }
.hints button { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; color: var(--text); text-align: left; }
.hints button:hover { border-color: var(--navy); }
.msg { margin: 0 0 16px; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 100%; }
.msg.user .bubble { background: var(--user); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--line); padding: 12px 16px; border-radius: 4px 16px 16px 16px; width: 100%; }
.msg.system .bubble { background: #FFF4EC; border: 1px solid #F6D2B8; color: #6A3412; padding: 10px 14px; border-radius: 12px; width: 100%; white-space: pre-wrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 3px 8px; font-size: 12.5px; color: var(--navy); text-decoration: none; max-width: 260px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { color: var(--muted); padding: 0 0 0 2px; font-size: 14px; }
.acts { display: flex; gap: 14px; margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--soft); font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.acts button { color: var(--navy); padding: 0; }
.acts .cost { margin-left: auto; }

/* markdown в ответе */
.md h1, .md h2, .md h3, .md h4 { color: var(--navy); margin: 14px 0 6px; line-height: 1.3; }
.md h1 { font-size: 19px; } .md h2 { font-size: 17px; } .md h3, .md h4 { font-size: 15.5px; }
.md h1:first-child, .md h2:first-child, .md h3:first-child { margin-top: 0; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-left: 22px; }
.md li { margin: 2px 0; }
.md li::marker { color: var(--rust); }
.md blockquote { margin: 8px 0; padding: 6px 12px; border-left: 3px solid var(--rust); background: #FAF6F5; color: #3D3330; }
.md code { background: var(--soft); border-radius: 4px; padding: 1px 4px; font-size: 13px; }
.md pre { background: #0F1A2E; color: #E8EEF7; padding: 10px 12px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; color: inherit; padding: 0; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.md .tw { overflow-x: auto; margin: 8px 0; -webkit-overflow-scrolling: touch; }
.md table { border-collapse: collapse; font-size: 13.5px; min-width: 100%; }
.md th { background: var(--navy2); color: #fff; text-align: left; font-weight: 600; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 8px; vertical-align: top; }
.md tr:nth-child(even) td { background: #F6F8FB; }

.typing { display: inline-flex; gap: 4px; align-items: center; color: var(--muted); }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--navy); opacity: .3; animation: b 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes b { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- ввод ---------- */
.composer { padding: 8px 12px calc(10px + var(--safe-b)); background: var(--bg); }
.composer .box { max-width: 860px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 8px; }
.composer .box:focus-within { border-color: var(--navy); }
.composer .row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; font: inherit; font-size: 16px; max-height: 40vh; padding: 6px 4px; background: none; }
.composer .attach { font-size: 22px; color: var(--navy); width: 38px; height: 38px; border-radius: 10px; flex: none; }
.composer .attach:hover { background: var(--soft); }
.composer .send { background: var(--navy); color: #fff; width: 38px; height: 38px; border-radius: 10px; font-size: 18px; flex: none; }
.composer .send:disabled { background: #9AA6B8; }
.composer .note { font-size: 12px; color: var(--muted); text-align: center; margin-top: 6px; }
.drop { position: fixed; inset: 0; background: rgba(0,38,83,.85); color: #fff; display: grid; place-items: center; font-size: 22px; z-index: 50; }

/* ---------- файлы ---------- */
.files { max-width: 860px; margin: 0 auto; }
.files .head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.files .head h2 { flex: 1; margin: 0; color: var(--navy); font-size: 18px; }
.btn { background: var(--navy); color: #fff; padding: 8px 14px; border-radius: 10px; }
.btn.light { background: var(--soft); color: var(--navy); }
.file { display: flex; align-items: center; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.file .ic { width: 38px; height: 38px; border-radius: 9px; background: var(--soft); color: var(--navy); display: grid; place-items: center; font-size: 11px; font-weight: 700; flex: none; }
.file .meta { flex: 1; min-width: 0; }
.file .meta b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file .meta small { color: var(--muted); }
.file .fa { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.file .fa button, .file .fa a { color: var(--navy); font-size: 13px; padding: 4px 8px; border-radius: 7px; text-decoration: none; }
.file .fa button:hover, .file .fa a:hover { background: var(--soft); }
.toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); background: var(--navy2); color: #fff; padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 60; }

/* ---------- телефон ---------- */
.scrim { display: none; }
@media (max-width: 820px) {
  .side { position: fixed; z-index: 40; inset: 0 auto 0 0; width: 84vw; max-width: 320px; transform: translateX(-100%); transition: transform .22s ease; }
  .side.open { transform: none; }
  .scrim.open { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 39; }
  .top .menu { display: block; }
  .seg button { padding: 5px 8px; }
  .msg.user .bubble { max-width: 92%; }
  .msg.assistant .bubble { padding: 10px 12px; }
  .file .fa .opt { display: none; }
}

/* ---------- вход ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; background: linear-gradient(160deg, var(--navy) 0%, var(--navy2) 100%); padding: 20px; }
.login form { background: var(--panel); width: 100%; max-width: 360px; border-radius: 18px; padding: 26px 22px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.login h1 { margin: 0 0 4px; color: var(--navy); font-size: 22px; }
.login p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.login label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.login input { width: 100%; font-size: 17px; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; }
.login input:focus { outline: 2px solid var(--navy); border-color: transparent; }
.login .code { letter-spacing: 6px; text-align: center; font-variant-numeric: tabular-nums; }
.login button { width: 100%; margin-top: 18px; background: var(--navy); color: #fff; padding: 12px; border-radius: 10px; font-weight: 600; font-size: 16px; }
.login .err { color: var(--rust); font-size: 14px; margin-top: 12px; min-height: 20px; }
.login .line { height: 3px; background: var(--rust); width: 48px; border-radius: 2px; margin-bottom: 14px; }
