/* AI Hub Mini App — premium light theme, Telegram CSS vars, European 2026 */

/* ── Token system ──────────────────────────────────────────────── */
:root {
  /* Telegram theme (fallbacks = light preview in browser) */
  --bg:          var(--tg-theme-bg-color,           #ffffff);
  --bg-2:        var(--tg-theme-secondary-bg-color, #f5f5f7);
  --bg-card:     var(--tg-theme-section-bg-color,   #ffffff);
  --text:        var(--tg-theme-text-color,         #111111);
  --text-muted:  var(--tg-theme-hint-color,         #8e8e93);
  --accent:      var(--tg-theme-button-color,       #007aff);
  --accent-text: var(--tg-theme-button-text-color,  #ffffff);
  --destructive: var(--tg-theme-destructive-text-color, #ff3b30);

  /* Derived */
  --border:      color-mix(in srgb, var(--text) 9%, transparent);
  --border-strong: color-mix(in srgb, var(--text) 15%, transparent);
  --shadow:      0 1px 4px color-mix(in srgb, var(--text) 8%, transparent);

  /* Geometry */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 20px;
  --r-pill: 999px;

  /* Typography */
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

/* ── Reset ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; padding: 0; }
textarea { font: inherit; color: inherit; }
p, h1, h2 { margin: 0; }

body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* ── Topbar ────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  height: 52px;
  flex-shrink: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.2px;
  flex-shrink: 0;
}

.model-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px 6px 8px;
  background: var(--bg-2);
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  flex: 1;
  min-width: 0;
  max-width: 180px;
  overflow: hidden;
  transition: background 120ms;
}
.model-btn:active { background: var(--border-strong); }
.model-btn-icon { display: flex; flex-shrink: 0; color: var(--text-muted); }
.model-btn-icon svg { display: block; }
.model-btn > span:nth-child(2) {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.model-btn-caret { flex-shrink: 0; color: var(--text-muted); }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  flex-shrink: 0;
}

.balance-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 9px;
  background: var(--bg-2);
  border-radius: var(--r-pill);
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  transition: background 120ms;
}
.balance-btn:active { background: var(--border-strong); }
.balance-num { color: var(--text); }

.icon-btn {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--r-pill);
  color: var(--text-muted);
  transition: background 120ms, color 120ms;
}
.icon-btn:active { background: var(--bg-2); color: var(--text); }

/* ── Content area ──────────────────────────────────────────────── */
.content {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

/* ── Welcome / empty state ─────────────────────────────────────── */
.welcome {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
  overflow-y: auto;
  transition: opacity 200ms ease;
}
.welcome.hidden {
  opacity: 0;
  pointer-events: none;
}
.welcome-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
  max-width: 340px;
  width: 100%;
}
.welcome-logo { flex-shrink: 0; }
.welcome-title {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.5px;
}
.welcome-sub {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 4px;
}
.suggestion-chip {
  padding: 7px 14px;
  background: var(--bg-2);
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  border: 1px solid var(--border);
  transition: background 120ms, border-color 120ms;
  white-space: nowrap;
}
.suggestion-chip:active {
  background: var(--bg-card);
  border-color: var(--accent);
}

/* ── Chat ──────────────────────────────────────────────────────── */
.chat {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 82%;
  padding: 10px 13px;
  border-radius: var(--r-lg);
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--bg-2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.msg.bot.streaming::after {
  content: '▋';
  display: inline;
  animation: blink 0.9s step-end infinite;
  color: var(--accent);
  margin-left: 1px;
}
.msg.system {
  align-self: center;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  max-width: 90%;
  text-align: center;
  padding: 4px 0;
}
.msg.img {
  align-self: flex-start;
  background: transparent;
  padding: 0;
  max-width: 88%;
}
.msg.img img {
  display: block;
  max-width: 100%;
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
}

/* Thinking / waiting for first token */
.msg.thinking {
  align-self: flex-start;
  background: var(--bg-2);
  border-bottom-left-radius: 4px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 14px 16px;
}
.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: dot-bounce 1.3s ease-in-out infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ── Composer ──────────────────────────────────────────────────── */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#input {
  flex: 1;
  resize: none;
  background: var(--bg-2);
  border: 1px solid transparent;
  border-radius: var(--r-lg);
  padding: 10px 13px;
  font-size: 15px;
  line-height: 1.5;
  max-height: 130px;
  outline: none;
  color: var(--text);
  transition: border-color 150ms;
  -webkit-appearance: none;
}
#input:focus {
  border-color: var(--accent);
}
#input::placeholder { color: var(--text-muted); }

.send-btn {
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: var(--r-md);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 150ms, transform 80ms;
}
.send-btn:active { transform: scale(0.93); }
.send-btn:disabled { opacity: 0.4; }
.send-btn svg { display: block; }

/* ── Bottom sheet ──────────────────────────────────────────────── */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
}
.sheet.hidden { display: none; }
.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}
.sheet-body {
  position: relative;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  background: var(--bg);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  padding: 12px 14px calc(16px + env(safe-area-inset-bottom, 0px));
  max-height: 82vh;
  overflow-y: auto;
  box-shadow: 0 -2px 24px rgba(0, 0, 0, 0.12);
}
.sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border-strong);
  border-radius: 2px;
  margin: 0 auto 16px;
}
.sheet-section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
  margin: 0 2px 8px;
}
.sheet-section-label + .model-list + .sheet-section-label,
.sheet-section-label ~ .sheet-section-label {
  margin-top: 16px;
}

/* Model cards */
.model-list, .tier-list { display: flex; flex-direction: column; gap: 6px; }

.model-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  background: var(--bg-2);
  border-radius: var(--r-md);
  border: 1.5px solid transparent;
  cursor: pointer;
  transition: border-color 120ms, background 120ms;
  -webkit-tap-highlight-color: transparent;
}
.model-card:active { background: var(--bg-card); }
.model-card.active {
  border-color: var(--accent);
  background: var(--bg-card);
}
.model-card-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--r-sm);
  background: var(--bg-card);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text);
}
.model-card.active .model-card-icon {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
}
.model-card-info { flex: 1; min-width: 0; }
.model-card-name {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-card-desc {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}
.model-card-cost {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  flex-shrink: 0;
  text-align: right;
}
.model-card.active .model-card-cost { color: var(--accent); }
.badge-free {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  padding: 1px 5px;
  border-radius: 4px;
  background: color-mix(in srgb, #34c759 15%, transparent);
  color: #2a9b47;
  margin-top: 2px;
}

/* Tier cards */
.tier-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 14px;
  background: var(--bg-2);
  border-radius: var(--r-md);
  border: 1.5px solid transparent;
  cursor: pointer;
  transition: border-color 120ms, background 120ms;
  -webkit-tap-highlight-color: transparent;
}
.tier-card:active { background: var(--bg-card); }
.tier-card.owned { border-color: var(--accent); cursor: default; }
.tier-card-name {
  font-size: 14px;
  font-weight: 600;
}
.tier-card-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.tier-card-action {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
}
.tier-card.owned .tier-card-action {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

.sheet-close-btn {
  width: 100%;
  margin-top: 16px;
  padding: 13px;
  background: var(--bg-2);
  border-radius: var(--r-md);
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  transition: background 120ms;
}
.sheet-close-btn:active { background: var(--border-strong); }
