:root {
  --mts-red: #e30611;
  --mts-red-dark: #b8050e;
  --ink: #101828;
  --ink-2: #475467;
  --ink-3: #98a2b3;
  --line: #e4e7ec;
  --bg: #f7f8fa;
  --card: #ffffff;
  --ok: #12b76a;
  --warn: #f79009;
  --code: #0f172a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 8px 24px rgba(16,24,40,.06);
  font-synthesis: none;
}

* { box-sizing: border-box; }
/* hidden должен побеждать display:flex/grid у модалок и панелей */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr 0;
  height: 100vh;
  max-width: 1320px;
  margin: 0 auto;
  background: var(--card);
  box-shadow: 0 0 0 1px var(--line);
}
.app.debug-open { grid-template-columns: 1fr 400px; }

/* ------------------------------- topbar ------------------------------- */
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--card);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { display: grid; place-items: center; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 15px; letter-spacing: .2px; }
.brand-text span { font-size: 12px; color: var(--ink-3); }
.topnav { display: flex; align-items: center; gap: 10px; }

button { font: inherit; cursor: pointer; border: 0; background: none; }
.ghost {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  color: var(--ink-2);
  background: var(--card);
  transition: .15s;
}
.ghost:hover { border-color: var(--ink-3); color: var(--ink); }
.ghost.link { text-decoration: none; font-size: 14px; }
.primary {
  background: var(--mts-red); color: #fff; border-radius: 10px;
  padding: 10px 16px; font-weight: 600; transition: .15s;
}
.primary:hover { background: var(--mts-red-dark); }

.switch { display: flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { display: none; }
.slider {
  width: 38px; height: 22px; border-radius: 999px; background: #d0d5dd;
  position: relative; transition: .18s;
}
.slider::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; transition: .18s; box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.switch input:checked + .slider { background: var(--mts-red); }
.switch input:checked + .slider::after { transform: translateX(16px); }
.switch-label { font-size: 13px; color: var(--ink-2); }

/* ------------------------------- chat --------------------------------- */
.chat {
  grid-column: 1;
  display: flex; flex-direction: column;
  overflow: hidden;
  min-height: 0;
}
.messages {
  flex: 1; overflow-y: auto; padding: 24px 20px 8px;
  display: flex; flex-direction: column; gap: 16px;
  scroll-behavior: smooth;
}
.msg { display: flex; gap: 10px; max-width: 780px; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.avatar {
  flex: 0 0 32px; width: 32px; height: 32px; border-radius: 10px;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
  background: #fee4e2; color: var(--mts-red);
}
.msg.user .avatar { background: #eef2f6; color: var(--ink-2); }
.bubble {
  background: var(--bg); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
  font-size: 15px; line-height: 1.55; white-space: pre-wrap; word-break: break-word;
}
.msg.user .bubble { background: var(--mts-red); color: #fff; border-color: transparent; }
.bubble p { margin: 0; }
.bubble p + p { margin-top: 8px; }

.meta-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-top: 8px; font-size: 12px; color: var(--ink-3);
}
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 999px; background: #f2f4f7;
  color: var(--ink-2); font-variant-numeric: tabular-nums;
}
.badge.ok { background: #ecfdf3; color: #027a48; }
.badge.warn { background: #fffaeb; color: #b54708; }
.badge.hit { background: #eff8ff; color: #175cd3; cursor: pointer; border: 0; }
.badge.hit:hover { background: #d1e9ff; }
.badge.cta {
  background: var(--mts-red); color: #fff; border: 0; font-weight: 600;
  padding: 4px 10px; cursor: pointer;
}
.badge.cta:hover { background: var(--mts-red-dark); }
.primary.small { padding: 7px 12px; font-size: 13px; }

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

/* ---------------------------- composer -------------------------------- */
.composer-wrap {
  padding: 12px 20px 16px; border-top: 1px solid var(--line); background: var(--card);
}
.suggestions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.chip {
  border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px;
  font-size: 13px; color: var(--ink-2); background: var(--card); transition: .15s;
}
.chip:hover { border-color: var(--mts-red); color: var(--mts-red); }
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  border: 1px solid var(--line); border-radius: 16px; padding: 8px 8px 8px 14px;
  background: var(--card); transition: .15s;
}
.composer:focus-within { border-color: var(--mts-red); box-shadow: 0 0 0 3px rgba(227,6,17,.08); }
.composer textarea {
  flex: 1; border: 0; resize: none; outline: none; font: inherit; font-size: 15px;
  line-height: 1.5; max-height: 160px; padding: 6px 0; background: transparent; color: var(--ink);
}
.send {
  width: 38px; height: 38px; border-radius: 12px; background: var(--mts-red);
  color: #fff; display: grid; place-items: center; transition: .15s;
}
.send:disabled { background: #d0d5dd; cursor: not-allowed; }
.send:not(:disabled):hover { background: var(--mts-red-dark); }
.composer-hint {
  display: flex; justify-content: space-between; margin-top: 8px;
  font-size: 12px; color: var(--ink-3);
}

/* --------------------------- debug panel ------------------------------ */
.debug-panel {
  grid-column: 2; border-left: 1px solid var(--line);
  background: #fbfcfd; display: flex; flex-direction: column; overflow: hidden;
}
.debug-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.debug-head h3 { margin: 0; font-size: 14px; }
.icon { font-size: 20px; line-height: 1; color: var(--ink-3); padding: 0 4px; }
.debug-body { overflow-y: auto; padding: 14px 16px; font-size: 13px; }
.debug-section { margin-bottom: 18px; }
.debug-section h4 {
  margin: 0 0 8px; font-size: 11px; letter-spacing: .08em;
  text-transform: uppercase; color: var(--ink-3);
}
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; }
.kv span:first-child { color: var(--ink-2); }
.kv span:last-child { font-variant-numeric: tabular-nums; font-weight: 600; }
.hit-card {
  border: 1px solid var(--line); border-radius: 10px; padding: 10px;
  margin-bottom: 8px; background: var(--card);
}
.hit-card .hit-title { font-weight: 600; font-size: 12.5px; margin-bottom: 2px; }
.hit-card .hit-path { font-size: 11.5px; color: var(--ink-3); margin-bottom: 6px; }
.hit-card .hit-preview { font-size: 12px; color: var(--ink-2); line-height: 1.45; }
.scorebar { height: 4px; border-radius: 2px; background: var(--line); margin-top: 8px; overflow: hidden; }
.scorebar > i { display: block; height: 100%; background: var(--mts-red); }
.spans { display: flex; flex-direction: column; gap: 4px; }
.span-row {
  display: grid; grid-template-columns: 1fr auto; gap: 8px;
  font-size: 12px; padding: 4px 0; border-bottom: 1px dashed var(--line);
}
.span-row code { font-size: 11.5px; color: var(--ink-2); }
.pre {
  background: var(--code); color: #e2e8f0; border-radius: 10px; padding: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11.5px;
  overflow-x: auto; white-space: pre-wrap; word-break: break-word;
}

/* ------------------------------ modal --------------------------------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(16,24,40,.5);
  display: grid; place-items: center; z-index: 50; padding: 16px;
}
.modal {
  background: var(--card); border-radius: 18px; padding: 24px;
  width: min(460px, 100%); box-shadow: var(--shadow);
}
.modal h2 { margin: 0 0 6px; font-size: 19px; }
.modal form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.modal label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--ink-2); }
.modal input, .modal textarea {
  font: inherit; font-size: 14px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); outline: none; color: var(--ink); background: var(--card);
}
.modal input:focus, .modal textarea:focus { border-color: var(--mts-red); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.muted { color: var(--ink-2); font-size: 13.5px; line-height: 1.5; margin: 0; }
.tiny { font-size: 11.5px; }
.footnote {
  grid-column: 1 / -1; padding: 8px 20px 12px; font-size: 11.5px;
  color: var(--ink-3); border-top: 1px solid var(--line); background: var(--card);
}

@media (max-width: 1000px) {
  .app.debug-open { grid-template-columns: 1fr; }
  .debug-panel { position: fixed; inset: auto 0 0 0; height: 60vh; z-index: 40; box-shadow: var(--shadow); }
  .messages { max-width: none; }
}
