:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #2a7ae2);
  --accent: var(--tg-theme-button-color, #2a7ae2);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #d93a3a);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--hint); }

#app { display: flex; flex-direction: column; height: 100vh; height: var(--tg-viewport-stable-height, 100dvh); }
main { flex: 1; min-height: 0; position: relative; }
.view { display: none; flex-direction: column; height: 100%; }
.view.active { display: flex; }

.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-bottom: 1px solid var(--bg2);
}
.bar h1 { flex: 1; margin: 0; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon { background: none; border: 0; font-size: 20px; width: 36px; height: 36px; border-radius: 50%; }
.icon:active { background: var(--bg2); }

/* Чат */
.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.msg {
  max-width: 85%; padding: 8px 12px; border-radius: var(--radius);
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--bg2); border-bottom-left-radius: 4px; }
.msg.pending { opacity: .6; }
.msg.error { align-self: center; background: none; color: var(--danger); text-align: center; font-size: 14px; }
.msg.error button { margin-top: 6px; }
.msg pre { background: rgba(0,0,0,.08); padding: 8px; border-radius: 8px; overflow-x: auto; white-space: pre; }
.msg code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.typing span { display: inline-block; width: 7px; height: 7px; margin: 0 2px; border-radius: 50%; background: var(--hint); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.composer { display: flex; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--bg2); background: var(--bg); }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; padding: 9px 12px;
  border: 1px solid var(--bg2); border-radius: 20px; background: var(--bg2); color: var(--text); font: inherit;
}
.composer button { width: 42px; height: 42px; border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-text); font-size: 18px; }
.composer button:disabled { opacity: .5; }

.composer .attach { background: none; color: var(--hint); font-size: 20px; }
.composer .attach:active { background: var(--bg2); }

.pending-files { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px 0; border-top: 1px solid var(--bg2); }
.file-chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 4px 8px; border-radius: 10px; background: var(--bg2); font-size: 13px;
}
.file-chip .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 180px; }
.file-chip.uploading { opacity: .6; }
.file-chip.error { color: var(--danger); }
.file-chip button { background: none; border: 0; padding: 0 2px; color: var(--hint); }
.msg .files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.msg.user .file-chip { background: rgba(255,255,255,.2); color: inherit; }
.msg img.thumb { display: block; max-width: 220px; max-height: 220px; border-radius: 10px; margin-top: 6px; background: rgba(0,0,0,.08); min-width: 60px; min-height: 60px; }

.promo-row { display: flex; gap: 8px; }
.promo-row input {
  flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--hint);
  background: var(--bg); color: var(--text); font: inherit; text-transform: uppercase;
}
.promo-row .primary { width: auto; padding: 10px 14px; }

.empty { margin: auto; text-align: center; color: var(--hint); padding: 24px; max-width: 320px; }

/* Списки и карточки */
.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.list li { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--bg2); }
.list li .title { flex: 1; min-width: 0; }
.list li .title div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list li small { color: var(--hint); }
.card { background: var(--bg2); border-radius: var(--radius); margin: 12px; padding: 14px 16px; }
.card h2 { margin: 0 0 8px; font-size: 16px; }
.primary, .danger { width: 100%; padding: 11px; border-radius: 10px; border: 0; font-weight: 600; }
.primary { background: var(--accent); color: var(--accent-text); }
.danger { background: none; color: var(--danger); border: 1px solid var(--danger); }

/* Паутина */
#graph-wrap { position: relative; flex: 1; min-height: 0; overflow: hidden; }
#graph { width: 100%; height: 100%; display: block; touch-action: none; }
#graph-wrap .empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.link { stroke: var(--hint); stroke-opacity: .45; }
.node circle { stroke: var(--bg); stroke-width: 2px; }
.node text { fill: var(--text); font-size: 11px; paint-order: stroke; stroke: var(--bg); stroke-width: 3px; pointer-events: none; }
.node.selected circle { stroke: var(--text); stroke-width: 3px; }
#legend { position: absolute; left: 8px; bottom: 8px; display: flex; flex-wrap: wrap; gap: 4px; max-width: calc(100% - 16px); pointer-events: none; }
#legend span { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--bg2); opacity: .9; }
#legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }

.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 60%; overflow-y: auto;
  background: var(--bg); border-top-left-radius: 18px; border-top-right-radius: 18px;
  box-shadow: 0 -4px 24px rgba(0,0,0,.18); padding: 16px;
}
.sheet-close { position: absolute; right: 8px; top: 8px; }
.sheet h2 { margin: 0 40px 4px 0; font-size: 18px; }
.sheet h3 { margin: 14px 0 6px; font-size: 14px; color: var(--hint); }
.chip { display: inline-block; margin: 0 6px 6px 0; padding: 4px 10px; border-radius: 12px; background: var(--bg2); border: 0; font-size: 13px; }

/* Нижняя навигация */
.tabs { display: flex; border-top: 1px solid var(--bg2); background: var(--bg); padding-bottom: env(safe-area-inset-bottom); }
.tabs button { flex: 1; background: none; border: 0; padding: 6px 0 8px; font-size: 18px; color: var(--hint); display: flex; flex-direction: column; align-items: center; }
.tabs button span { font-size: 11px; }
.tabs button.active { color: var(--accent); }

.toast {
  position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%);
  background: rgba(0,0,0,.8); color: #fff; padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 10;
  max-width: 90%;
}
