/* Кеша-чат: тема, каркас, сообщения */
:root {
  --accent: #ff7a1a;
  --accent-dark: #e0660f;
  --accent-soft: rgba(255, 122, 26, .13);
  --radius: 14px;
}

[data-bs-theme="dark"] {
  --app-bg: #15171c;
  --panel: #1c1f26;
  --panel-2: #232833;
  --border: rgba(255, 255, 255, .08);
  --text: #e7eaf0;
  --muted: #98a1b0;
  --user-bubble: #2b3241;
  --code-bg: #0f1116;
}

[data-bs-theme="light"] {
  --app-bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f2f4f7;
  --border: rgba(16, 24, 40, .10);
  --text: #1c2333;
  --muted: #697386;
  --user-bubble: #eef1f6;
  --code-bg: #f4f5f7;
}

* { scrollbar-width: thin; }

body.app-body,
body.login-body {
  background: var(--app-bg);
  color: var(--text);
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.btn-accent:hover, .btn-accent:focus { background: var(--accent-dark); border-color: var(--accent-dark); color: #fff; }

.btn-ghost {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  border-radius: 10px;
}
.btn-ghost:hover { background: var(--panel-2); color: var(--text); }

/* ------------------------------------------------------------- login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100dvh; padding: 20px; }
.login-wrap { width: 100%; max-width: 400px; }
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px;
  text-align: center;
}
.login-logo { font-size: 42px; line-height: 1; margin-bottom: 10px; }
.login-card .form-control {
  background: var(--panel-2);
  border-color: var(--border);
  color: var(--text);
}
.login-card .form-control:focus {
  background: var(--panel-2);
  color: var(--text);
  border-color: var(--accent);
  box-shadow: 0 0 0 .2rem var(--accent-soft);
}

/* -------------------------------------------------------------- shell */
.app-shell { display: flex; height: 100dvh; overflow: hidden; }

.sidebar {
  width: 274px;
  flex: 0 0 274px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform .2s ease;
  z-index: 1045;
}
.sidebar-head { padding: 16px 16px 10px; }
.brand { font-size: 18px; font-weight: 700; display: flex; align-items: center; gap: 8px; }

.chat-list { flex: 1 1 auto; overflow-y: auto; padding: 6px 10px 12px; }
.chat-group-title { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding: 12px 8px 4px; }
.chat-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; border-radius: 10px; cursor: pointer;
  font-size: 14px; color: var(--text);
}
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chat-item .title { flex: 1 1 auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .tools { display: none; gap: 2px; }
.chat-item:hover .tools { display: flex; }
.chat-item .tools button {
  background: transparent; border: 0; color: var(--muted);
  padding: 2px 4px; border-radius: 6px; line-height: 1;
}
.chat-item .tools button:hover { color: var(--accent); background: rgba(255, 122, 26, .12); }

.sidebar-foot { border-top: 1px solid var(--border); padding: 12px; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--accent); color: #fff; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

.sidebar-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .5);
  opacity: 0; pointer-events: none; transition: opacity .2s ease; z-index: 1040;
}
.sidebar-backdrop.show { opacity: 1; pointer-events: auto; }

/* --------------------------------------------------------------- main */
.chat-main { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; }

.chat-top {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.chat-title { font-weight: 600; font-size: 15px; }
.model-select { max-width: 220px; background: var(--panel-2); border-color: var(--border); color: var(--text); }

/* кружок заполнения контекстного окна */
.ctx-btn {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; padding: 0; flex: 0 0 auto;
}
.ctx-ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.ctx-bg { fill: none; stroke: var(--border); stroke-width: 3; }
.ctx-fg {
  fill: none; stroke: #4ade80; stroke-width: 3; stroke-linecap: round;
  transition: stroke-dashoffset .35s ease, stroke .2s ease;
}
.ctx-label {
  position: relative; font-size: 10px; font-weight: 600; color: var(--muted);
  letter-spacing: -.02em;
}
.ctx-btn.ctx-warn .ctx-fg { stroke: #fbbf24; }
.ctx-btn.ctx-warn .ctx-label { color: #fbbf24; }
.ctx-btn.ctx-danger .ctx-fg { stroke: #f87171; }
.ctx-btn.ctx-danger .ctx-label { color: #f87171; }
@media (max-width: 575.98px) {
  .ctx-btn { width: 32px; height: 32px; }
  .ctx-label { font-size: 9px; }
}

.messages { flex: 1 1 auto; overflow-y: auto; padding: 24px 16px 8px; }
.messages-inner { max-width: 820px; margin: 0 auto; }

.msg { display: flex; gap: 12px; padding: 12px 0; }
.msg .who {
  width: 32px; height: 32px; flex: 0 0 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-size: 17px;
}
.msg-assistant .who { background: var(--accent-soft); }
.msg-user { justify-content: flex-end; }
.msg-user .who { order: 2; background: var(--panel-2); font-size: 14px; font-weight: 700; }
.msg-body { max-width: 78%; min-width: 0; }
.msg-user .msg-body { order: 1; }
.msg-user .bubble, .msg-user .md {
  background: var(--user-bubble); border-radius: 14px; padding: 10px 14px;
}
.msg .meta { font-size: 11px; color: var(--muted); margin-top: 6px; display: flex; gap: 10px; align-items: center; }
.msg .meta button { background: none; border: 0; color: var(--muted); font-size: 11px; padding: 0; }
.msg .meta button:hover { color: var(--accent); }

/* overflow-wrap: anywhere — иначе длинная строка без пробелов (ответ модели одним абзацем,
   код в ```text) раздувает флекс-элемент и уезжает за экран вместе со всей страницей. */
.md { line-height: 1.62; font-size: 15px; overflow-wrap: anywhere; word-break: break-word; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote { margin: 0 0 .7em; }
.md h1, .md h2, .md h3 { font-size: 17px; font-weight: 700; margin: 1.1em 0 .5em; }
.md ul, .md ol { padding-left: 1.3em; }
.md pre {
  background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; overflow-x: auto; margin: 0 0 .7em; position: relative;
  max-width: 100%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.md pre code { background: none; padding: 0; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
.md code { background: var(--code-bg); padding: 2px 5px; border-radius: 5px; font-size: 13.5px; font-family: Consolas, Menlo, monospace; }
.md table { width: 100%; border-collapse: collapse; margin-bottom: .7em; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 9px; }
.md a { color: var(--accent); }
.md blockquote { border-left: 3px solid var(--accent); padding-left: 12px; color: var(--muted); }
.copy-code {
  position: absolute; top: 8px; right: 8px; font-size: 11px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--muted);
  border-radius: 6px; padding: 2px 7px;
}
.copy-code:hover { color: var(--accent); }
/* Кнопка «копировать» абсолютная — без отступа сверху она лежит на первой строке текста. */
.md pre.has-copy { padding-top: 26px; }

.thinking {
  margin: 0 0 .6em; padding: 6px 10px;
  border: 1px dashed var(--border); border-radius: 10px;
  background: var(--panel-2); color: var(--muted); font-size: 13px;
}
.thinking summary { cursor: pointer; font-size: 12.5px; }
.thinking pre {
  margin: 6px 0 0; padding: 0; border: 0; background: none;
  white-space: pre-wrap; font-size: 12.5px; max-height: 220px; overflow: auto;
  color: var(--muted); font-family: inherit;
}

.retry-bar { padding: 6px 0 2px; }
.retry-bar .btn { font-size: 13px; }

.cursor::after {
  content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.welcome { padding: 8vh 0 0; text-align: center; }
.welcome .logo { font-size: 46px; }
.welcome h2 { font-size: 22px; font-weight: 700; margin: 12px 0 6px; }
.welcome p { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 8px 14px; font-size: 13.5px;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* ----------------------------------------------------------- composer */
.composer { padding: 10px 16px 14px; }
.composer-inner {
  max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 8px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 10px 10px 8px 12px;
}
.composer-inner:focus-within { border-color: var(--accent); box-shadow: 0 0 0 .18rem var(--accent-soft); }
.composer-row { display: flex; gap: 6px; align-items: flex-end; }
.icon-btn { padding: 7px 9px; font-size: 17px; line-height: 1; border-radius: 12px; }
.icon-btn.recording { color: #fff; background: #d9483b; }
#input {
  flex: 1 1 auto; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font-size: 15px; line-height: 1.5; max-height: 220px; padding: 6px 0;
}
.send-btn { border-radius: 12px; padding: 7px 13px; font-size: 16px; }
.composer-hint { text-align: center; font-size: 11.5px; color: var(--muted); margin-top: 8px; }

/* ------------------------------------------------- вложения в composer */
.attach-preview { display: none; flex-wrap: wrap; gap: 8px; }
.attach-preview.show { display: flex; }
.attach-item {
  position: relative; display: flex; align-items: center; gap: 6px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 4px 24px 4px 6px; max-width: 100%;
}
.attach-item img { width: 44px; height: 44px; object-fit: cover; border-radius: 7px; display: block; }
.attach-item .ai-icon { font-size: 20px; }
.attach-item .ai-label {
  font-size: 12px; color: var(--muted); max-width: 220px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.attach-item .ai-del {
  position: absolute; top: 2px; right: 2px; border: 0; background: transparent;
  color: var(--muted); font-size: 15px; line-height: 1; padding: 0 4px; border-radius: 50%;
}
.attach-item .ai-del:hover { color: #d9483b; }
.attach-item.loading { color: var(--muted); font-size: 12px; padding: 4px 10px; }
.attach-warn {
  flex-basis: 100%; font-size: 12px; line-height: 1.35; color: #9a6700;
  background: #fff8e1; border: 1px solid #f0d98c; border-radius: 7px; padding: 4px 9px;
}

.rec-bar {
  display: none; align-items: center; gap: 10px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 7px 10px; font-size: 13px;
}
.rec-bar.show { display: flex; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #d9483b; animation: recpulse 1s infinite; }
@keyframes recpulse { 50% { opacity: .25; } }
.rec-time { font-variant-numeric: tabular-nums; font-weight: 600; }
.rec-hint { color: var(--muted); flex: 1 1 auto; }

/* ------------------------------------------------- вложения в переписке */
.attach-box { display: flex; flex-direction: column; gap: 6px; margin-bottom: 6px; }
.msg-assistant .attach-box { margin-bottom: 0; }
.attach-box a { display: block; }
.attach-img {
  max-width: 100%; max-height: 340px; border-radius: 12px; display: block;
  border: 1px solid var(--border);
}
.attach-audio { display: flex; flex-direction: column; gap: 4px; }
.attach-audio audio { width: 100%; max-width: 320px; height: 36px; }
.attach-text { font-size: 13px; color: var(--muted); font-style: italic; max-width: 320px; }
.msg-user .attach-text { color: var(--text); opacity: .8; }
.bubble .md-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.retry-bar .status, .retry-note .status { font-variant-numeric: tabular-nums; }
.retry-note { padding: 6px 0 2px; }

@media (max-width: 991.98px) {
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; transform: translateX(-102%); }
  .sidebar.open { transform: none; }
  .messages { padding: 14px 10px 4px; }
  .msg-body { max-width: 92%; }
  .composer { padding: 8px 10px 10px; }
  .rec-hint { display: none; }
}

/* ---------------------------------------------------------- статистика */
.stats-wrap { max-width: 1100px; margin: 0 auto; padding: 22px 18px 60px; }
.stats-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.stats-title { font-size: 21px; font-weight: 700; margin: 0; flex: 1 1 auto; letter-spacing: -.2px; }
.stats-user { display: flex; align-items: center; gap: 8px; }
.avatar-sm { width: 28px; height: 28px; font-size: 12px; }

.stats-period { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.period-btn {
  background: var(--panel); border: 1px solid var(--border); color: var(--muted);
  border-radius: 999px; padding: 5px 14px; font-size: 13px; transition: .15s;
}
.period-btn:hover { color: var(--text); border-color: var(--accent); }
.period-btn.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.stat-cards { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); }
.stat-value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 11.5px; color: var(--muted); margin-top: 2px; min-height: 15px; }
.stat-card-cost .stat-value { color: var(--accent); }

.stat-panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.stat-panel-head h2 { font-size: 14px; font-weight: 600; margin: 0; }
.stat-legend { display: flex; gap: 12px; font-size: 11.5px; color: var(--muted); }
.stat-legend span { display: inline-flex; align-items: center; gap: 5px; }
.dot { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }
.dot-prompt { background: #56627a; }
.dot-output { background: var(--accent); }

.chart-box { width: 100%; }
.day-svg { width: 100%; height: 230px; display: block; overflow: visible; }
.day-svg .grid { stroke: var(--border); stroke-width: 1; }
.day-svg .axis { fill: var(--muted); font-size: 10px; }
.day-svg .bar-prompt { fill: #56627a; }
.day-svg .bar-output { fill: var(--accent); }
.day-svg .bar-empty { fill: var(--border); }
.day-svg rect { transition: opacity .15s; }
.day-svg g:hover rect { opacity: .75; }

.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 14px; }

.model-list { display: flex; flex-direction: column; gap: 12px; }
.model-row-head { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; }
.model-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-total { font-variant-numeric: tabular-nums; color: var(--muted); }
.model-bar { height: 6px; background: var(--panel-2); border-radius: 4px; margin: 5px 0 4px; overflow: hidden; }
.model-bar-fill { height: 100%; background: linear-gradient(90deg, var(--accent-dark), var(--accent)); border-radius: 4px; }
.model-meta { display: flex; gap: 10px; flex-wrap: wrap; font-size: 11.5px; color: var(--muted); }
.model-group { padding: 0 7px; border-radius: 999px; background: var(--panel-2); }
.model-cost { color: var(--accent); }
.model-free { color: #4ade80; }
.model-share { margin-left: auto; }

.chat-stats-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.chat-stats-table th { text-align: left; color: var(--muted); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: .3px; padding: 0 8px 6px; }
.chat-stats-table td { padding: 7px 8px; border-top: 1px solid var(--border); }
.chat-stats-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.chat-stats-table .title-cell { max-width: 210px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-stats-table .title-cell a { color: var(--text); text-decoration: none; }
.chat-stats-table .title-cell a:hover { color: var(--accent); }
.chat-stats-table .model-cell { color: var(--muted); max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.empty-note { color: var(--muted); font-size: 13px; padding: 10px 0; }
.stats-note { color: var(--muted); font-size: 11.5px; line-height: 1.5; margin-top: 18px; max-width: 780px; }

@media (max-width: 860px) {
  .stat-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats-grid { grid-template-columns: 1fr; }
  .stat-value { font-size: 19px; }
  .stats-wrap { padding: 16px 12px 48px; }
}
