:root {
  --bg: #0b0d10;
  --panel: #12151a;
  --panel-2: #181c23;
  --border: #2a3038;
  --text: #eef2f7;
  --muted: #9aa4b2;
  --accent: #6ea8fe;
  --accent-2: #4c8bf5;
  --danger: #ff6b6b;
  --success: #51cf66;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 Inter, system-ui, sans-serif;
}

body.chat-page {
  height: 100%;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-2); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 13, 16, 0.95);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { font-weight: 700; color: var(--text); font-size: 1.1rem; }
.topnav { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.user-chip { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); }
.avatar { width: 28px; height: 28px; border-radius: 50%; }

.page { min-height: calc(100vh - 58px); }

body.chat-page .page {
  min-height: 0;
  height: calc(100vh - 58px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  font: inherit;
}
.btn.primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #081018;
  font-weight: 600;
}
.btn.ghost { background: transparent; }
.btn.large { padding: 12px 20px; font-size: 1.05rem; }
.btn.full { width: 100%; }

.flash-wrap { padding: 12px 20px 0; }
.flash {
  padding: 10px 12px;
  border-radius: 10px;
  margin-bottom: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
}
.flash.error { border-color: rgba(255, 107, 107, 0.5); color: #ffd6d6; }
.flash.success { border-color: rgba(81, 207, 102, 0.5); color: #d7ffe0; }

.hero {
  max-width: 720px;
  margin: 80px auto;
  padding: 0 24px;
  text-align: center;
}
.hero h1 { font-size: 3rem; margin: 0 0 12px; }
.lead { color: var(--muted); font-size: 1.15rem; }
.features {
  list-style: none;
  padding: 0;
  margin: 28px 0 32px;
  color: var(--muted);
}
.features li { margin: 8px 0; }

.settings-page {
  max-width: 760px;
  margin: 32px auto;
  padding: 0 24px 48px;
}

.settings-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(5, 7, 10, 0.72);
  backdrop-filter: blur(4px);
}

.settings-overlay[hidden] { display: none !important; }

body.settings-open { overflow: hidden; }

.settings-panel {
  width: min(100%, 560px);
  max-height: min(90vh, 820px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.settings-panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.settings-panel-header h2 {
  margin: 0;
  font-size: 1.35rem;
}

.settings-panel-hint {
  margin: 6px 0 0;
}

.settings-close {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.settings-close:hover {
  color: var(--text);
  border-color: var(--accent);
  background: rgba(110, 168, 254, 0.08);
}

.settings-panel-body {
  overflow: auto;
  padding: 16px 20px 24px;
  flex: 1;
}

.settings-loading {
  padding: 24px 0;
  text-align: center;
}

.settings-status.success { color: var(--success); }
.settings-status.error { color: var(--danger); }

.nav-settings-btn {
  font: inherit;
  cursor: pointer;
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
}

.nav-settings-btn:hover { color: var(--accent-2); }

.settings-form {
  display: grid;
  gap: 12px;
  margin-top: 20px;
}
.settings-form select,
.settings-form input[type="search"] {
  width: 100%;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
}
.settings-form select.model-select { min-height: 280px; }

.model-filter-group {
  display: grid;
  gap: 8px;
}

.model-filter-label {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.model-filter-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.model-filter-tab {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.model-filter-tab:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.model-filter-tab.active {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #081018;
  font-weight: 600;
}

.model-picker {
  display: grid;
  gap: 8px;
  max-height: 280px;
  overflow: auto;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
}

.model-picker-item {
  display: grid;
  gap: 4px;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  font: inherit;
}

.model-picker-item:hover {
  border-color: var(--border);
}

.model-picker-item.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px rgba(110, 168, 254, 0.35);
}

.model-picker-title {
  font-weight: 600;
  line-height: 1.3;
}

.model-picker-meta {
  color: var(--muted);
  font-size: 0.82rem;
  word-break: break-all;
}

.model-picker-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.model-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.75rem;
  line-height: 1.4;
  border: 1px solid var(--border);
  color: var(--muted);
}

.model-badge-text { color: #b2f2bb; border-color: rgba(81, 207, 102, 0.35); }
.model-badge-image { color: #a5d8ff; border-color: rgba(110, 168, 254, 0.35); }
.model-badge-video { color: #ffd8a8; border-color: rgba(255, 169, 77, 0.35); }
.model-badge-country { color: var(--muted); }

.model-picker-empty {
  margin: 0;
}

#settings-model-description {
  white-space: pre-line;
}
.muted { color: var(--muted); }
.small { font-size: 0.9rem; }

.chat-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  flex: 1;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}
.chat-sidebar {
  border-right: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.sidebar-head { padding: 16px; border-bottom: 1px solid var(--border); }
.conversation-list { overflow: auto; flex: 1; padding: 8px; }
.conversation-item {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text);
  margin-bottom: 6px;
  border: 1px solid transparent;
}
.conversation-item:hover,
.conversation-item.active {
  background: var(--panel-2);
  border-color: var(--border);
}
.conversation-item .title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conversation-item .meta {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  margin-top: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-empty { padding: 12px; }

.chat-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}
.chat-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}
.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.message {
  max-width: 860px;
  padding: 14px 16px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
}
.message.user { align-self: flex-end; background: #152033; }
.message.assistant { align-self: flex-start; }
.message-role {
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.message-body { white-space: pre-wrap; word-break: break-word; }
.message.streaming .message-body {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 28px;
}
.typing-label {
  color: var(--muted);
  font-size: 0.95rem;
}
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.35;
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.35; }
  40% { transform: translateY(-4px); opacity: 1; }
}
.attachments { margin-top: 10px; display: grid; gap: 8px; }
.message .attachments:empty { display: none; margin-top: 0; }
.attachment-image-wrap {
  display: inline-grid;
  gap: 6px;
  max-width: min(100%, 420px);
}
.attachment-image {
  max-width: min(100%, 420px);
  border-radius: 10px;
  border: 1px solid var(--border);
}
.attachment-image-wrap .attachment-image { max-width: 100%; }
.attachment-download {
  font-size: 13px;
  color: var(--accent);
  width: fit-content;
}
.attachment-download:hover { color: var(--accent-2); text-decoration: underline; }
.attachment-text summary { cursor: pointer; color: var(--accent); }
.attachment-text pre {
  white-space: pre-wrap;
  max-height: 240px;
  overflow: auto;
  background: var(--bg);
  padding: 10px;
  border-radius: 8px;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 14px 18px 18px;
  background: rgba(11, 13, 16, 0.98);
  flex-shrink: 0;
}
.composer-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: end;
}
.composer textarea {
  width: 100%;
  resize: vertical;
  min-height: 52px;
  max-height: 220px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
}
.file-btn {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  background: var(--panel);
}
.file-btn input { display: none; }
.composer-files {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}
.composer-files[hidden] {
  display: none;
}
.upload-preview {
  position: relative;
  display: grid;
  gap: 6px;
  width: 112px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
}
.upload-preview-image {
  width: 100%;
  height: 72px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.upload-preview-file {
  width: 100%;
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--panel-2);
  font-size: 1.8rem;
}
.upload-preview-meta {
  min-width: 0;
}
.upload-preview-name {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.upload-preview-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.72);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}
.upload-preview-remove:hover {
  background: rgba(180, 40, 40, 0.92);
}
.attachment-file-chip {
  display: inline-block;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 0.9rem;
}
.file-chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.85rem;
  color: var(--muted);
}

.coin-icon-xs { width: 18px; height: 18px; flex-shrink: 0; display: block; vertical-align: -4px; }
.coin-icon-sm { width: 28px; height: 28px; flex-shrink: 0; display: block; }
.coin-icon { width: 72px; height: 72px; flex-shrink: 0; display: block; filter: drop-shadow(0 8px 14px rgba(0, 0, 0, 0.35)); }

.balance-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid rgba(244, 185, 66, 0.35);
  background: rgba(244, 185, 66, 0.08);
  color: #ffd98a;
  font-weight: 600;
}
.balance-chip:hover { background: rgba(244, 185, 66, 0.16); color: #ffe6a8; }

.wallet-page {
  max-width: 920px;
  margin: 0 auto;
  padding: 28px 24px 48px;
}
.wallet-hero {
  display: flex;
  align-items: center;
  gap: 24px;
  margin-bottom: 28px;
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: linear-gradient(135deg, rgba(244, 185, 66, 0.08), rgba(110, 168, 254, 0.05));
}
.coin-stack { position: relative; width: 92px; height: 80px; flex-shrink: 0; }
.coin-stack .coin-back {
  position: absolute;
  left: 8px;
  top: 10px;
  width: 64px;
  height: 64px;
  opacity: 0.55;
  transform: rotate(-12deg);
}
.coin-stack .coin-front {
  position: absolute;
  left: 18px;
  top: 0;
  width: 72px;
  height: 72px;
}
.wallet-balance {
  margin: 0;
  font-size: 2.4rem;
  line-height: 1.1;
}
.coin-word { color: #f4b942; font-size: 1.4rem; font-weight: 600; }
.wallet-note { margin: 0 0 16px; }
.topup-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}
.topup-card {
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  background: var(--panel);
  display: grid;
  gap: 10px;
}
.topup-coins {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.2rem;
}
.topup-bonus { color: var(--success); font-size: 0.92rem; }
.topup-price { color: var(--muted); font-size: 1.05rem; }
.wallet-history {
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
}
.wallet-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.wallet-row:last-child { border-bottom: none; }
.wallet-amount {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
  white-space: nowrap;
}
.wallet-amount.plus { color: #8ce99a; }
.wallet-amount.minus { color: #ffb3b3; }

@media (max-width: 900px) {
  .chat-layout { grid-template-columns: 1fr; }
  .chat-sidebar { display: none; }
}

.telegram-bootstrap {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  padding-top: calc(24px + env(safe-area-inset-top));
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.telegram-bootstrap-card {
  width: min(360px, 100%);
  text-align: center;
  padding: 28px 20px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--panel);
}

.telegram-bootstrap-logo {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 12px;
}

.telegram-bootstrap-error {
  color: var(--danger);
  margin: 0;
}

body.telegram-app .topbar {
  padding-top: calc(12px + env(safe-area-inset-top));
}

body.telegram-app.chat-page .page {
  height: calc(100vh - 58px - env(safe-area-inset-top));
  height: calc(100dvh - 58px - env(safe-area-inset-top));
}

body.telegram-app .user-chip {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

html.telegram-webapp body.telegram-app {
  background: var(--tg-bg, var(--bg));
  color: var(--tg-text, var(--text));
}

@media (max-width: 900px) {
  body.chat-page .page {
    height: calc(100vh - 58px);
  }
}
