:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #1d1f23;
  --muted: #6b6f76;
  --line: #e1ddd3;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --user: #e3ece8;
  --ok: #3d8a6a;
  --warn: #c58a1a;
  --bad: #c0463b;
  --code: #f1efe9;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .05);
  --chart: #1f7a55;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b; --surface: #1d2025; --surface-2: #262a30; --text: #e8e6e1; --muted: #9aa0a8;
    --line: #2f343b; --accent: #6fb39a; --accent-text: #0f1512; --user: #24332d; --code: #23272d;
    --ok: #5fb38e; --warn: #d9a441; --bad: #e0675c; --shadow: none; --chart: #2ea370; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171b; --surface: #1d2025; --surface-2: #262a30; --text: #e8e6e1; --muted: #9aa0a8;
  --line: #2f343b; --accent: #6fb39a; --accent-text: #0f1512; --user: #24332d; --code: #23272d;
  --ok: #5fb38e; --warn: #d9a441; --bad: #e0675c; --shadow: none; --chart: #2ea370; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, select, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); margin: 0; }

/* вход */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px 24px; display: grid; gap: 14px; box-shadow: var(--shadow);
}
.login-card h1 { margin: 0; font-size: 26px; }
.login-card p { margin: 0; font-size: 14px; }
.login-card input {
  padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 16px;
}
.primary {
  padding: 12px 16px; border-radius: 10px; border: 0; background: var(--accent); color: var(--accent-text);
  font-weight: 600; cursor: pointer;
}

/* каркас */
.app { height: 100%; display: flex; flex-direction: column; max-width: 820px; margin: 0 auto; }
.top {
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.top select {
  flex: 1; min-width: 0; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; font-size: 16px;
}
.icon-btn { width: 40px; height: 40px; border: 0; background: transparent; border-radius: 10px; font-size: 20px; cursor: pointer; flex: none; }
.icon-btn:active, .pill:active { background: var(--surface-2); }
.pill { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 12px; font-size: 14px; cursor: pointer; flex: none; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; cursor: pointer; padding: 2px 4px; font-size: 13px; }

/* контекст */
.ctx { padding: 6px max(14px, env(safe-area-inset-left)) 6px; border-bottom: 1px solid var(--line); font-size: 12.5px; color: var(--muted); }
.ctx-bar { height: 4px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.ctx-bar span { display: block; height: 100%; width: 0; background: var(--ok); transition: width .3s; }
.ctx[data-level="warn"] .ctx-bar span { background: var(--warn); }
.ctx[data-level="bad"] .ctx-bar span { background: var(--bad); }
.ctx-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 4px; font-variant-numeric: tabular-nums; }
.ctx-hint { margin-top: 4px; color: var(--warn); }
.ctx[data-level="bad"] .ctx-hint { color: var(--bad); }

/* лента */
.log { flex: 1; overflow-y: auto; padding: 16px max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left)); -webkit-overflow-scrolling: touch; }
.msg { margin: 0 0 12px; max-width: 100%; overflow-wrap: anywhere; }
.msg.user {
  margin-left: auto; width: fit-content; max-width: 88%; background: var(--user); padding: 9px 13px;
  border-radius: 16px 16px 4px 16px; white-space: pre-wrap;
}
.msg.assistant { background: var(--surface); border: 1px solid var(--line); padding: 10px 14px; border-radius: 4px 16px 16px 16px; box-shadow: var(--shadow); }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: .5em 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { font-size: 1.05em; margin: .8em 0 .4em; }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.3em; margin: .4em 0; }
.msg.assistant table { border-collapse: collapse; margin: .6em 0; display: block; overflow-x: auto; max-width: 100%; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.msg.assistant th { background: var(--surface-2); }
.msg.assistant code { background: var(--code); padding: 1px 5px; border-radius: 5px; font-size: .9em; font-family: ui-monospace, Menlo, monospace; }
.msg.assistant pre { background: var(--code); padding: 10px 12px; border-radius: 10px; overflow-x: auto; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.assistant blockquote { margin: .5em 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.thinking { color: var(--muted); font-style: italic; }
.thinking::after { content: "…"; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .2; } }

.tool { font-size: 12.5px; color: var(--muted); margin: -4px 0 8px 6px; display: flex; gap: 6px; }
.tool.write { color: var(--accent); }
.tool.error { color: var(--bad); }
.audio {
  display: flex; align-items: center; gap: 10px; margin: 0 0 12px; padding: 8px 12px 8px 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; width: fit-content; max-width: 100%;
}
.audio button {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text);
  font-size: 15px; cursor: pointer; flex: none;
}
.audio .a-text { font-style: italic; }
.audio .a-meta { font-size: 12px; color: var(--muted); }
.stats { font-size: 11.5px; color: var(--muted); margin: -6px 0 14px 6px; font-variant-numeric: tabular-nums; }
.marker { text-align: center; font-size: 12.5px; color: var(--muted); margin: 14px 0; }
.marker details { text-align: left; margin-top: 6px; background: var(--surface-2); border-radius: 10px; padding: 8px 12px; white-space: pre-wrap; }
.err { color: var(--bad); background: color-mix(in srgb, var(--bad) 10%, transparent); padding: 8px 12px; border-radius: 10px; margin: 0 0 12px; font-size: 14px; }

.empty { text-align: center; margin: 12vh auto 0; max-width: 420px; padding: 0 12px; }
.empty h2 { margin: 0 0 6px; font-size: 22px; }
.empty p { margin: 0 0 18px; color: var(--muted); }

/* ввод */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 8px max(12px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  border-top: 1px solid var(--line); background: var(--bg);
}
.composer textarea {
  flex: 1; resize: none; max-height: 40vh; padding: 10px 14px; border-radius: 20px; border: 1px solid var(--line);
  background: var(--surface); font-size: 16px; line-height: 1.4;
}
.send { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text); font-size: 20px; font-weight: 700; cursor: pointer; flex: none; }
.send:disabled { opacity: .4; }

/* боковая панель */
.drawer { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 10; }
.drawer-panel {
  position: absolute; top: 0; bottom: 0; left: 0; width: min(340px, 88vw); background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top)) 12px 12px max(12px, env(safe-area-inset-left)); overflow-y: auto;
}
.drawer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.chat-item { display: block; width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; cursor: pointer; }
.chat-item.active { border-color: var(--accent); }
.chat-item .t { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .m { font-size: 12px; color: var(--muted); }

/* диалоги */
.dlg { border: 0; border-radius: 16px; padding: 0; width: min(440px, 92vw); background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
.dlg::backdrop { background: rgba(0, 0, 0, .35); }
.dlg form { padding: 20px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; }
.dlg h3 { margin: 0; font-size: 18px; }
.dlg p { margin: 0; color: var(--muted); font-size: 14px; }
.dlg label { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; font-size: 14px; min-width: 0; }
.dlg select, .dlg input { width: 100%; min-width: 0; text-overflow: ellipsis; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 16px; }
.dlg .row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.dlg .row button { padding: 9px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; }
.dlg .row button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.dlg .row button.danger { color: var(--bad); }
.dlg .left { justify-content: flex-start; }

/* шапка: на узком экране у «Новый чат» остаётся только плюс */
@media (max-width: 440px) { .pill-label { display: none; } .pill { padding: 6px 11px; font-size: 16px; } }
.top-title { flex: 1; font-size: 17px; }

/* статистика */
.stats-body { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.st-sec { margin: 0 0 28px; }
.st-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.st-sec h2 { font-size: 20px; margin: 0; }
.st-sec h4 { font-size: 14px; margin: 16px 0 6px; }
.small { font-size: 13px; }
.note { font-size: 12.5px; color: var(--muted); margin: 6px 0 0; }
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 10px 0 14px; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.t-label { font-size: 12.5px; color: var(--muted); }
.t-value { font-size: 22px; font-weight: 600; }
.t-sub { font-size: 12px; color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin-bottom: 10px; }
.card h3 { margin: 0; font-size: 17px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.chip { font-size: 12px; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.chip.on { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--text); }
.level { font-size: 15px; font-weight: 600; margin-top: 6px; }
.meta { font-size: 13px; color: var(--muted); }
.sub-label { font-size: 12px; color: var(--muted); margin: 10px 0 3px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chips span { font-size: 12.5px; background: var(--surface-2); padding: 2px 8px; border-radius: 8px; }
.card ul { margin: 0; padding-left: 1.2em; font-size: 14px; }
.card p { margin: 0; font-size: 14px; }
.card .summary { margin-top: 10px; color: var(--muted); font-size: 13.5px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-bottom: 14px; }
.hero { font-size: 30px; font-weight: 600; line-height: 1.2; }
.meter { height: 8px; background: color-mix(in srgb, var(--chart) 18%, var(--surface-2)); border-radius: 6px; overflow: hidden; margin: 8px 0 4px; }
.meter span { display: block; height: 100%; background: var(--chart); border-radius: 6px; }
.meter[data-level="warn"] span { background: var(--warn); }
.meter[data-level="bad"] span { background: var(--bad); }
.chart-title { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: inherit; }
.chart .bar { fill: var(--chart); transition: opacity .15s; }
.chart .bar.hover { opacity: .7; }
.chart .hit { fill: transparent; outline: none; }
.tip { position: fixed; z-index: 30; pointer-events: none; background: var(--text); color: var(--bg); padding: 6px 9px; border-radius: 8px; font-size: 12.5px; white-space: nowrap; }
.tip b { font-weight: 700; }
.tbl-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 2px 8px; margin-bottom: 14px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { padding: 7px 6px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl th:first-child, .tbl td:first-child { text-align: left; white-space: normal; }
.tbl th { color: var(--muted); font-weight: 500; font-size: 12px; }
details.more summary { font-size: 13px; color: var(--muted); cursor: pointer; margin: 6px 0 2px; }

/* правка и перегенерация */
.user-wrap { display: flex; flex-direction: column; align-items: flex-end; margin: 0 0 12px; }
.user-wrap .msg.user { margin: 0; }
.msg-act { border: 0; background: none; color: var(--muted); font-size: 12.5px; padding: 4px 6px; cursor: pointer; border-radius: 8px; }
.msg-act:hover, .msg-act:focus-visible { color: var(--text); background: var(--surface-2); }
.msg-actions { margin: -8px 0 14px 0; }
.edit-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; color: var(--muted);
  padding: 6px max(14px, env(safe-area-inset-right)) 6px max(14px, env(safe-area-inset-left)); border-top: 1px solid var(--line); background: var(--surface-2);
}
.panel p { margin: 4px 0; font-size: 14px; }
.panel p.note, .panel p.small { font-size: 12.5px; }
.panel p.small { font-size: 13px; }
