:root {
  --bg: #0b0d12;
  --bg-soft: #12151d;
  --bg-panel: #161a24;
  --border: #242a38;
  --text: #e8ecf4;
  --muted: #8b93a7;
  --accent: #6c8cff;
  --accent-2: #9b6cff;
  --ok: #34d399;
  --err: #f87171;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.btn {
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { border-color: #3a4256; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
}
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.small { padding: 7px 12px; font-size: 13px; }
.btn.danger { color: var(--err); border-color: #4b2a2f; }

.link {
  background: none;
  border: 0;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  padding: 0;
}
.link:hover { text-decoration: underline; }

.icon-btn {
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
}
.icon-btn:hover { color: var(--text); background: var(--bg-soft); }

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
  width: 100%;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }

/* ---------- вход ---------- */
.auth-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(600px 400px at 20% 0%, rgba(108, 140, 255, .18), transparent 60%),
    radial-gradient(700px 500px at 90% 100%, rgba(155, 108, 255, .14), transparent 60%);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: rgba(22, 26, 36, .9);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .45);
}
.auth-brand, .brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  letter-spacing: .2px;
}
.auth-brand { margin-bottom: 18px; color: var(--muted); font-weight: 600; font-size: 14px; }
.logo-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 14px rgba(108, 140, 255, .8);
}
.auth-card h1 { margin: 0 0 6px; font-size: 24px; }
.auth-card form { margin-top: 20px; display: grid; gap: 14px; }
.auth-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.auth-switch { margin-top: 16px; font-size: 13px; }
.form-error { color: var(--err); font-size: 13px; min-height: 0; }

/* ---------- приложение ---------- */
.app { display: flex; height: 100vh; }

.sidebar {
  width: 268px;
  flex: 0 0 268px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
}
.new-chat { width: 100%; }
.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0 -6px;
  padding: 0 6px;
}
.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13.5px;
}
.chat-item:hover { background: var(--bg-panel); color: var(--text); }
.chat-item.active { background: var(--bg-panel); color: var(--text); }
.chat-item .title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-item .del {
  opacity: 0;
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 4px;
  border-radius: 6px;
}
.chat-item:hover .del { opacity: 1; }
.chat-item .del:hover { color: var(--err); }

.side-bottom {
  border-top: 1px solid var(--border);
  padding-top: 12px;
  display: grid;
  gap: 8px;
}
.balance-line { display: flex; justify-content: space-between; align-items: baseline; font-size: 13.5px; }
.user-line { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 12.5px; }
.user-line .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(900px 400px at 70% -10%, rgba(108, 140, 255, .07), transparent 65%),
    var(--bg);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}
.topbar select { width: auto; min-width: 220px; max-width: 340px; padding: 8px 10px; font-size: 13.5px; }
.topbar-title { color: var(--muted); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.msg { max-width: 780px; width: 100%; margin: 0 auto; display: flex; gap: 12px; }
.msg .avatar {
  flex: 0 0 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 600;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  color: var(--muted);
}
.msg.user .avatar { background: rgba(108, 140, 255, .16); color: #b9c7ff; border-color: rgba(108, 140, 255, .3); }
.msg .bubble {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  min-width: 0;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: rgba(108, 140, 255, .1); border-color: rgba(108, 140, 255, .22); }
.bubble.md { white-space: normal; }
.msg .meta { font-size: 11.5px; color: var(--muted); margin-top: 6px; }
.bubble pre {
  background: #0a0c11;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 8px 0;
  white-space: pre;
}
.bubble code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; }
.bubble :not(pre) > code {
  background: #0a0c11;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
}

.empty {
  margin: auto;
  text-align: center;
  max-width: 460px;
  color: var(--muted);
  padding: 40px 20px;
}
.empty h2 { color: var(--text); font-size: 22px; margin: 0 0 8px; }
.empty .chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 20px; }
.chip {
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); }

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

.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
  padding: 12px 20px 20px;
}
.composer textarea { resize: none; max-height: 180px; line-height: 1.45; }
.composer .btn { flex: 0 0 auto; height: 42px; }

/* ---------- модальные окна ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 11, .72);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 50;
}
.modal-card {
  width: 100%;
  max-width: 860px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 30px 90px rgba(0, 0, 0, .6);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 20px; overflow-y: auto; }

.banner {
  margin: 10px 20px 0;
  border: 1px solid rgba(108, 140, 255, .35);
  background: rgba(108, 140, 255, .12);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
}

.pay-box { border: 1px solid var(--border); border-radius: 12px; padding: 14px; margin-bottom: 16px; }
.pay-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 10px; }
.pay-row input { width: 150px; }
.pay-hint { font-size: 12.5px; color: var(--muted); margin-top: 8px; }
h3.section { margin: 20px 0 10px; font-size: 15px; }
.auth-switch { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.tabs { display: flex; gap: 6px; margin-bottom: 18px; flex-wrap: wrap; }
.tab {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  padding: 7px 14px;
  border-radius: 9px;
  cursor: pointer;
  font: inherit;
  font-size: 13.5px;
}
.tab.active { background: var(--bg-soft); color: var(--text); border-color: #3a4256; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 12.5px; }
tr:last-child td { border-bottom: 0; }
td .btn { padding: 5px 10px; font-size: 12.5px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 18px; }
.stat-card { background: var(--bg-soft); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.stat-card .v { font-size: 20px; font-weight: 650; margin-top: 2px; }
.stat-card .k { font-size: 12px; color: var(--muted); }

.form-grid { display: grid; gap: 12px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid label { display: grid; gap: 6px; font-size: 12.5px; color: var(--muted); }
.hint-box {
  background: var(--bg-soft);
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 12.5px;
  color: var(--muted);
}
.provider-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 10px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
}
.provider-card .models { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.badge { display: inline-block; font-size: 11.5px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.badge.ok { color: var(--ok); border-color: rgba(52, 211, 153, .35); }
.badge.off { color: var(--err); border-color: rgba(248, 113, 113, .3); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: #1d2330;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13.5px;
  z-index: 100;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .4);
}
.toast.err { border-color: rgba(248, 113, 113, .5); }

@media (max-width: 760px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%;
    flex: 0 0 auto;
    max-height: 42vh;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .chat-list { max-height: 120px; }
  .topbar select { min-width: 0; max-width: 60%; }
  .form-row { grid-template-columns: 1fr; }
  .modal { padding: 10px; }
}
