:root {
  color-scheme: dark;
  --bg: #0d0f14;
  --bg2: #141821;
  --bg3: #1c212c;
  --line: #262c39;
  --text: #e7eaf0;
  --muted: #8a93a6;
  --accent: #4fd1c5;
  --accent2: #38a3a5;
  --user: #1f3b4d;
  --danger: #f26d6d;
  --radius: 16px;
  --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: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif; }
body { overflow: hidden; }
button, input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.error { color: var(--danger); min-height: 1.5em; }
#app, .screen { height: 100%; }

/* ---------- кнопки ---------- */
.btn { background: var(--bg3); border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; cursor: pointer; }
.btn:hover { border-color: #3a4254; }
.btn.primary { background: var(--accent); color: #062421; border-color: transparent; font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }
.icon-btn { background: none; border: 0; width: 40px; height: 40px; border-radius: 12px; font-size: 20px;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; color: var(--text); }
.icon-btn:hover { background: var(--bg3); }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0 4px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
input[type=text], input:not([type]), textarea.edit { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; outline: none; width: 100%; }
input:focus, textarea:focus { border-color: var(--accent2); }

/* ---------- вход ---------- */
#login { display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-box { width: 100%; max-width: 340px; text-align: center; display: flex; flex-direction: column; gap: 12px; }
.login-box h1 { margin: 8px 0 0; font-weight: 600; letter-spacing: .5px; }
#setup-block { display: flex; flex-direction: column; gap: 10px; }
.logo-ring { width: 84px; height: 84px; margin: 0 auto; border-radius: 50%;
  background: radial-gradient(circle, rgba(79,209,197,.35) 0 30%, transparent 32%),
              conic-gradient(from 0deg, var(--accent), #2b6cb0, var(--accent));
  -webkit-mask: radial-gradient(circle, #000 0 30%, transparent 31% 40%, #000 41%);
          mask: radial-gradient(circle, #000 0 30%, transparent 31% 40%, #000 41%);
  box-shadow: 0 0 40px rgba(79,209,197,.25); animation: spin 12s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- раскладка ---------- */
#main { display: flex; }
#drawer { width: 290px; flex-shrink: 0; background: var(--bg2); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding-top: var(--safe-t); }
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 12px 8px 16px; }
.brand { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.nav { display: flex; flex-direction: column; padding: 4px 8px; gap: 2px; }
.nav button, .chat-item { text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 10px; cursor: pointer; }
.nav button:hover, .chat-item:hover { background: var(--bg3); }
.drawer-title { padding: 14px 18px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
#chat-list { overflow-y: auto; flex: 1; padding: 0 8px calc(12px + var(--safe-b)); }
.chat-item { display: block; width: 100%; }
.chat-item.active { background: var(--bg3); }
.chat-item .t { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .p { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#scrim { display: none; }
#content { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100%; }
#topbar { display: flex; align-items: center; gap: 4px; padding: calc(6px + var(--safe-t)) 8px 6px; border-bottom: 1px solid var(--line);
  background: rgba(13,15,20,.85); backdrop-filter: blur(10px); }
#title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
#btn-menu { visibility: hidden; }

@media (max-width: 820px) {
  #drawer { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .22s ease;
    width: min(86vw, 320px); }
  #main.drawer-open #drawer { transform: none; }
  #main.drawer-open #scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
  #btn-menu { visibility: visible; }
}

.banner { background: #13323a; color: #cdeff0; font-size: 14px; padding: 8px 12px; display: flex; gap: 6px; align-items: center; }
.banner .close { margin-left: auto; color: var(--muted); }

/* ---------- чат ---------- */
.view { flex: 1; min-height: 0; }
#view-chat { display: flex; flex-direction: column; }
#messages { flex: 1; overflow-y: auto; padding: 16px 12px 8px; display: flex; flex-direction: column; gap: 10px;
  overscroll-behavior: contain; }
.msg { max-width: min(760px, 92%); padding: 10px 14px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--bg2); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.reminder { border-color: #3d6b4f; background: #142019; }
.msg.error { border-color: var(--danger); color: var(--danger); }
.msg p { margin: .4em 0; } .msg p:first-child { margin-top: 0; } .msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: .4em 0; padding-left: 1.3em; }
.msg pre { background: #0a0c10; border: 1px solid var(--line); border-radius: 10px; padding: 10px; overflow-x: auto; font-size: 13px; }
.msg code { font-family: ui-monospace, Menlo, monospace; font-size: .9em; background: #0a0c10; padding: 1px 5px; border-radius: 5px; }
.msg pre code { background: none; padding: 0; }
.msg a { color: var(--accent); }
.msg h1, .msg h2, .msg h3 { font-size: 1.05em; margin: .6em 0 .3em; }
.msg table { border-collapse: collapse; font-size: 14px; display: block; overflow-x: auto; }
.msg td, .msg th { border: 1px solid var(--line); padding: 4px 8px; }
.msg .files { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.msg .files img { max-width: 200px; max-height: 200px; border-radius: 10px; display: block; }
.file-chip { display: inline-flex; gap: 6px; align-items: center; background: rgba(255,255,255,.07); border-radius: 8px;
  padding: 4px 8px; font-size: 13px; color: var(--text); text-decoration: none; }
.tool-note { align-self: flex-start; font-size: 12px; color: var(--muted); padding: 0 6px; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }
.confirm { align-self: flex-start; background: #2a2414; border: 1px solid #6b5a2a; border-radius: var(--radius); padding: 10px 14px;
  display: flex; flex-direction: column; gap: 8px; max-width: 92%; }
.confirm .row button { min-width: 80px; }
.empty { margin: auto; text-align: center; color: var(--muted); padding: 24px; }
.empty .logo-ring { width: 56px; height: 56px; margin-bottom: 12px; }

#composer { padding: 8px 10px calc(8px + var(--safe-b)); border-top: 1px solid var(--line); background: var(--bg); }
.composer-row { display: flex; align-items: flex-end; gap: 6px; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 22px; padding: 4px; }
#input { flex: 1; resize: none; border: 0; outline: 0; background: none; padding: 8px 4px; max-height: 40vh; line-height: 1.4;
  font-size: 16px; }
.send { width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: #062421; font-size: 20px;
  font-weight: 700; cursor: pointer; flex-shrink: 0; }
.send:disabled { background: var(--bg3); color: var(--muted); }
.attach { flex-shrink: 0; width: 38px; height: 38px; border-radius: 50%; }
#attach-preview { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 4px 6px; }
#attach-preview:empty { display: none; }
#attach-preview .file-chip img { width: 28px; height: 28px; object-fit: cover; border-radius: 4px; }
#attach-preview .file-chip button { background: none; border: 0; color: var(--muted); cursor: pointer; }

/* ---------- страницы ---------- */
.page { overflow-y: auto; padding: 16px 16px calc(24px + var(--safe-b)); }
.page > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.page h2 { margin: 4px auto 6px; font-weight: 600; }
.card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; }
.card h3 { margin: 0 0 8px; font-size: 16px; }
.item { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.item .meta { font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.item .actions { margin-left: auto; display: flex; gap: 2px; }
.item .actions button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 6px; border-radius: 6px; }
.item .actions button:hover { background: var(--bg3); color: var(--text); }
.cat { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--accent); }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--bg3); }
#fact-add { margin: 12px auto; }
.kbd { border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }
.log-row { border-bottom: 1px solid var(--line); padding: 6px 0; word-break: break-word; }
.log-row code { font-size: 12px; color: var(--muted); }

#toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%); background: var(--bg3);
  border: 1px solid var(--line); padding: 8px 14px; border-radius: 12px; z-index: 50; font-size: 14px; max-width: 90vw; }
