:root {
  --bg: #f7f6f3;
  --panel: #ffffff;
  --text: #1f1e1c;
  --muted: #6f6b64;
  --border: #e3e0d9;
  --accent: #c2643c;
  --accent-text: #ffffff;
  --user-bg: #ece8df;
  --code-bg: #f1efe9;
  --error: #b42318;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b19;
    --panel: #252421;
    --text: #ece9e2;
    --muted: #a39e94;
    --border: #3a3833;
    --accent: #d9774b;
    --accent-text: #1c1b19;
    --user-bg: #33312c;
    --code-bg: #2e2c28;
    --error: #f97066;
  }
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
}

button, select, input, textarea {
  font: inherit;
  color: inherit;
}

button {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}

button:disabled { opacity: .5; cursor: default; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

select, input, textarea {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
}

a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--error); }

/* ------------------------------------------------ вход */
.login-page {
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-card h1 { margin: 0; }
.login-card p { margin: 0; }

/* ------------------------------------------------ боковая панель */
#sidebar {
  width: 270px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 10px;
}

#chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  border-radius: 8px;
}

.chat-item.active, .chat-item:hover { background: var(--user-bg); }

.chat-title {
  flex: 1;
  min-width: 0;
  text-align: left;
  border: 0;
  background: transparent;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-delete {
  border: 0;
  background: transparent;
  color: var(--muted);
  visibility: hidden;
}

.chat-item:hover .chat-delete, .chat-item.active .chat-delete { visibility: visible; }

.sidebar-footer {
  display: flex;
  gap: 8px;
}

.sidebar-footer button { flex: 1; }

/* ------------------------------------------------ чат */
main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

header label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
}

#month-cost { margin-left: auto; font-size: 13px; }
#toggle-sidebar { display: none; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#empty-state {
  max-width: 560px;
  margin: 40px auto;
  text-align: center;
}

.notice {
  background: var(--user-bg);
  border-radius: 8px;
  padding: 8px 12px;
  align-self: center;
  max-width: 760px;
}

.msg {
  width: 100%;
  max-width: 820px;
  align-self: center;
  overflow-wrap: anywhere;
}

.msg.user {
  background: var(--user-bg);
  border-radius: 14px;
  padding: 10px 14px;
  white-space: pre-wrap;
  max-width: 680px;
  align-self: flex-end;
  width: auto;
}

.msg.assistant .content > :first-child { margin-top: 0; }
.msg.assistant .content > :last-child { margin-bottom: 0; }

.msg pre {
  background: var(--code-bg);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  white-space: pre;
}

.msg code {
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--code-bg);
  border-radius: 4px;
  padding: 1px 4px;
}

.msg pre code { background: none; padding: 0; }

.msg blockquote {
  margin: 8px 0;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

.table-wrap { overflow-x: auto; }

table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 14px;
}

th, td {
  border: 1px solid var(--border);
  padding: 4px 10px;
  text-align: left;
  vertical-align: top;
}

.thinking {
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 8px;
}

.thinking summary { cursor: pointer; }
.thinking-text { white-space: pre-wrap; padding: 6px 0 0 14px; }

.typing { color: var(--muted); }

.meta {
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
}

.meta .copy {
  font-size: 12px;
  padding: 1px 8px;
}

.note, .msg .error {
  margin-top: 8px;
  font-size: 14px;
}

.note { color: var(--muted); }

#composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

#composer textarea {
  flex: 1;
  resize: vertical;
  min-height: 48px;
  max-height: 40vh;
}

/* ------------------------------------------------ расходы */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  padding: 20px;
}

dialog::backdrop { background: rgba(0, 0, 0, .4); }

.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-head h2 { margin: 0; }

.usage-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin: 14px 0;
}

.usage-cards div {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
}

.usage-cards b { font-size: 22px; }

dialog h3 { margin: 18px 0 4px; font-size: 15px; }

/* ------------------------------------------------ телефон */
@media (max-width: 800px) {
  #toggle-sidebar { display: inline-block; }

  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform .2s;
    box-shadow: 0 0 24px rgba(0, 0, 0, .25);
  }

  body.sidebar-open #sidebar { transform: none; }

  #month-cost { width: 100%; margin-left: 0; }
  #messages { padding: 14px 12px; }
  #composer { padding: 10px 12px 12px; }
  .chat-delete { visibility: visible; }
}
