:root {
  --bg: #fbfaf7;
  --panel: #f2efe8;
  --surface: #ffffff;
  --text: #211e18;
  --muted: #6e685c;
  --line: #ddd8cc;
  --accent: #9a6210;
  --accent-fill: #e8a33d;
  --on-accent: #17140f;
  --user-bubble: #efe9dc;
  --code-bg: #24211b;
  --code-text: #f1ece1;
  --danger: #b3261e;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15130f;
    --panel: #1d1a15;
    --surface: #24211b;
    --text: #efeade;
    --muted: #a59e8f;
    --line: #37322a;
    --accent: #eeb25c;
    --user-bubble: #2d2920;
    --code-bg: #0e0d0a;
    --danger: #ff8a80;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .06em; }
.brand-large { font-size: 22px; }
.mark {
  display: inline-block; width: 22px; height: 25px; background: var(--accent-fill);
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
.mark-large { width: 44px; height: 50px; }

.primary, .secondary {
  border-radius: 10px; padding: 10px 16px; cursor: pointer; font-weight: 600; white-space: nowrap;
  min-height: 44px;
}
.primary { background: var(--accent-fill); color: var(--on-accent); border: 1px solid var(--accent-fill); }
.primary:disabled { opacity: .55; cursor: default; }
.secondary { background: transparent; border: 1px solid var(--line); }
.link { background: none; border: 0; padding: 6px 0; color: var(--accent); cursor: pointer; text-align: left; }
.danger { color: var(--danger); }
.error { color: var(--danger); margin: 0; font-size: 14px; }

/* Вход */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(400px, 100%); display: grid; gap: 14px; }
.auth-card h1 { font-size: 26px; margin: 12px 0 2px; line-height: 1.2; }
.auth-card label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.auth-card input[type=email], .auth-card input[type=password] {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 12px; color: var(--text); font-size: 16px; width: 100%;
}
.auth-card .check { grid-template-columns: 20px 1fr; align-items: start; gap: 10px; }
.auth-card .check input { margin: 4px 0 0; width: 18px; height: 18px; }
.auth-card .link { text-align: center; }

/* Приложение */
.app { height: 100dvh; display: grid; grid-template-columns: 272px minmax(0, 1fr); }
.sidebar {
  background: var(--panel); border-right: 1px solid var(--line); padding: 16px;
  display: flex; flex-direction: column; gap: 14px; min-height: 0;
}
.new-chat {
  border: 1px solid var(--line); background: var(--surface); border-radius: 10px;
  padding: 10px 12px; text-align: left; cursor: pointer; font-weight: 600; min-height: 44px;
}
.chat-list { flex: 1; overflow-y: auto; display: grid; gap: 2px; align-content: start; min-height: 0; }
.chat-item {
  background: none; border: 0; border-radius: 8px; padding: 9px 10px; text-align: left; cursor: pointer;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; min-height: 40px;
}
.chat-item:hover { background: var(--user-bubble); }
.chat-item.active { background: var(--user-bubble); font-weight: 600; }
.account { border-top: 1px solid var(--line); padding-top: 12px; font-size: 13px; color: var(--muted); }
.account-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }

.main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 20px; min-height: 56px;
  border-bottom: 1px solid var(--line);
}
.chat-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-button { display: none; background: none; border: 0; font-size: 20px; width: 44px; height: 44px; cursor: pointer; }
.demo-banner { background: var(--accent-fill); color: var(--on-accent); padding: 6px 20px; font-size: 13px; }

.messages { flex: 1; overflow-y: auto; padding: 24px 20px; display: flex; flex-direction: column; gap: 20px; }
.empty { margin: auto; max-width: 420px; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); margin: 14px 0 6px; font-size: 26px; line-height: 1.2; }
.empty p { margin: 4px 0; }
.empty .disclaimer { margin-top: 14px; font-size: 13px; }

.message { width: min(760px, 100%); align-self: center; overflow-wrap: anywhere; }
.message.user {
  background: var(--user-bubble); border-radius: var(--radius); padding: 10px 14px;
  white-space: pre-wrap;
}
.message p { margin: 0 0 10px; }
.message p:last-child { margin-bottom: 0; }
.message ul, .message ol { margin: 0 0 10px; padding-left: 22px; }
.message h3, .message h4 { margin: 14px 0 6px; line-height: 1.3; }
.message code { background: var(--user-bubble); padding: 1px 5px; border-radius: 5px; font: .92em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.code-block { position: relative; margin: 0 0 10px; }
.code-block pre {
  background: var(--code-bg); color: var(--code-text); padding: 14px; border-radius: 10px;
  overflow-x: auto; margin: 0; font: 13.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.code-block code { background: none; padding: 0; font: inherit; }
.copy {
  position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 3px 8px; border-radius: 6px; cursor: pointer;
  background: var(--surface); color: var(--text); border: 1px solid var(--line);
}
.message.stopped::after { content: "Ответ остановлен"; display: block; margin-top: 6px; font-size: 13px; color: var(--muted); }

.composer { padding: 10px 20px calc(14px + env(safe-area-inset-bottom)); display: grid; gap: 8px; justify-items: center; }
.composer .error { width: min(760px, 100%); }
.composer-box {
  width: min(760px, 100%); display: flex; align-items: flex-end; gap: 8px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; padding: 8px;
}
.composer-box textarea {
  flex: 1; min-width: 0; resize: none; border: 0; background: transparent; outline: none;
  padding: 9px 8px; max-height: 200px; font-size: 16px;
}

.subscribe { width: 100%; margin: 10px 0 4px; }

.pay {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .55);
  display: grid; grid-template-columns: minmax(0, 1fr); justify-items: center; align-items: center; padding: 16px; overflow-y: auto;
}
.pay-card {
  position: relative; width: min(440px, 100%); background: var(--bg); border: 1px solid var(--line);
  border-radius: 16px; padding: 20px;
}
.pay-close { position: absolute; top: 12px; right: 20px; min-height: 44px; }
.pay-card h2 { font-size: 20px; margin: 18px 0 4px; line-height: 1.3; }
.pay-rate { margin: 0 0 16px; color: var(--muted); font-size: 14px; }
.pay-amount-label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.pay-amount {
  display: flex; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 4px 14px; font-size: 28px; font-weight: 700;
}
.pay-amount:focus-within { border-color: var(--accent); }
.pay-amount input { flex: 1; width: 100%; min-width: 0; border: 0; background: transparent; outline: none; font: inherit; padding: 6px 0; }
.pay-presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0 12px; }
.pay-presets button {
  min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 600;
}
.pay-presets button:hover { border-color: var(--accent); }
.pay-result { margin: 0 0 4px; font-weight: 600; }
.balance { display: flex; align-items: baseline; gap: 6px; margin-top: 8px; color: var(--text); }
.balance-label { color: var(--muted); }
.balance-value { font-size: 20px; font-weight: 700; }
.balance-unit { color: var(--muted); }
.pay-price { font-size: 32px; font-weight: 700; line-height: 1.2; }
.pay-price small { font-size: 15px; font-weight: 400; color: var(--muted); }
.pay-card ul { margin: 14px 0; padding-left: 20px; color: var(--muted); font-size: 15px; }
.pay-test { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: var(--user-bubble); font-size: 14px; }
.pay-go { width: 100%; }
.paid-row { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; color: var(--text); }
.notice { background: var(--user-bubble); padding: 8px 20px; font-size: 14px; }
.pay-legal { margin: 10px 0 0; font-size: 12px; color: var(--muted); }

.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 10; }

@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(290px, 85vw); z-index: 11;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .sidebar.open { transform: none; }
  .icon-button { display: block; margin-left: -10px; }
  .topbar { padding: 6px 16px; }
  .messages { padding: 16px; }
  .composer { padding-left: 16px; padding-right: 16px; }
  .demo-banner { padding: 6px 16px; }
  .pay { align-items: start; }
  }

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
