@font-face {
  font-family: 'Timeless Sans';
  src: url('/fonts/TimelessSansVF.woff2') format('woff2-variations'),
       url('/fonts/TimelessSansVF.woff2') format('woff2');
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #ffffff;
  --surface: #f7f7f8;
  --surface-2: #efeff1;
  --border: #e2e2e6;
  --text: #111114;
  --muted: #666670;
  --accent: #111114;
  --accent-text: #ffffff;
  --user-bg: #111114;
  --user-text: #ffffff;
  --danger: #d92d20;
  --danger-bg: rgba(217, 45, 32, 0.08);
  --code-bg: #ededf0;
  --font-sans: 'Timeless Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #0b0b0d;
  --surface: #141418;
  --surface-2: #1c1c22;
  --border: #27272f;
  --text: #f3f3f5;
  --muted: #9a9aa6;
  --accent: #f3f3f5;
  --accent-text: #0b0b0d;
  --user-bg: #f3f3f5;
  --user-text: #0b0b0d;
  --danger: #f97066;
  --danger-bg: rgba(249, 112, 102, 0.12);
  --code-bg: #1f1f26;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.chat-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

/* Header */
.chat-header {
  flex: 0 0 auto;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  padding: 0.75rem 1rem;
}

.chat-header-inner {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.brand-dot {
  width: 10px;
  height: 10px;
  background: var(--text);
  flex-shrink: 0;
}

.brand-title {
  font-weight: 700;
  font-size: 0.98rem;
  line-height: 1.2;
}

.brand-sub {
  font-size: 0.76rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 44vw;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.btn-icon,
.btn-new-chat {
  font-family: var(--font-sans);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font-size: 0.84rem;
  font-weight: 600;
  padding: 0.42rem 0.75rem;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn-icon {
  width: 34px;
  height: 34px;
  padding: 0;
}

.btn-icon:hover,
.btn-new-chat:hover {
  background: var(--surface-2);
  border-color: var(--muted);
}

/* Main Scroll Area */
.chat-main {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 1.25rem 1rem 1.5rem;
}

.chat-container {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
}

/* Welcome State */
.welcome-card {
  margin: 3rem auto 1rem;
  max-width: 620px;
  text-align: center;
  padding: 1.5rem 1rem;
}

.welcome-title {
  font-size: clamp(1.35rem, 3vw, 1.8rem);
  font-weight: 700;
  margin: 0 0 0.65rem;
  line-height: 1.2;
}

.welcome-copy {
  color: var(--muted);
  font-size: 0.94rem;
  margin: 0 0 1.4rem;
}

.starter-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
}

.starter-chip {
  font-family: var(--font-sans);
  font-size: 0.85rem;
  font-weight: 500;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.55rem 0.85rem;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.starter-chip:hover {
  background: var(--surface-2);
  border-color: var(--muted);
}

/* Message Bubbles */
.msg-row {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.msg-row.user {
  align-items: flex-end;
}

.msg-row.assistant {
  align-items: stretch;
}

.bubble-user {
  background: var(--user-bg);
  color: var(--user-text);
  padding: 0.7rem 1rem;
  max-width: 82%;
  font-size: 0.94rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble-assistant {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0.9rem 1.05rem;
  font-size: 0.94rem;
  word-break: break-word;
}

.bubble-label {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 0.35rem;
}

/* Markdown Typography inside Assistant Replies */
.md-body > *:first-child { margin-top: 0; }
.md-body > *:last-child { margin-bottom: 0; }
.md-body p { margin: 0 0 0.65rem; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4 {
  font-weight: 700;
  margin: 0.9rem 0 0.4rem;
  line-height: 1.3;
}
.md-body h1 { font-size: 1.15rem; }
.md-body h2 { font-size: 1.05rem; }
.md-body h3 { font-size: 0.98rem; }
.md-body ul, .md-body ol {
  margin: 0 0 0.65rem;
  padding-left: 1.35rem;
}
.md-body li { margin-bottom: 0.25rem; }
.md-body blockquote {
  margin: 0.6rem 0;
  padding: 0.25rem 0.85rem;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.md-body pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  padding: 0.75rem 0.9rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  margin: 0.65rem 0;
}
.md-body code,
.inline-code {
  font-family: var(--font-mono);
  font-size: 0.84em;
  background: var(--code-bg);
  padding: 0.1rem 0.32rem;
}
.md-body pre code {
  background: transparent;
  padding: 0;
}
.md-body a {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* Typing Indicator */
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0.25rem 0;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: var(--muted);
  display: inline-block;
  animation: arBounce 1.1s infinite ease-in-out both;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.16s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.32s; }

@keyframes arBounce {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

/* Contextual Ad Card (Horizontal Image Left, Aspect Ratio Preserved, Full-Card Clickable) */
.ad-card {
  display: block;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 0.85rem 1rem;
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.ad-card-link {
  cursor: pointer;
}

.ad-card-link:hover,
.ad-card-link:focus-visible {
  border-color: var(--text);
  background: var(--surface);
  outline: none;
}

.ad-card-link:hover .ad-cta {
  opacity: 0.88;
}

.ad-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
}

.ad-brand {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  min-width: 0;
}

.ad-favicon {
  width: 18px;
  height: 18px;
  object-fit: contain;
  flex-shrink: 0;
}

.ad-avatar {
  width: 18px;
  height: 18px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.68rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.ad-brand-name {
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ad-badge {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.12rem 0.42rem;
  flex-shrink: 0;
}

.ad-body {
  display: flex;
  align-items: flex-start;
  gap: 0.9rem;
  margin-bottom: 0.75rem;
}

.ad-media {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
  max-width: 168px;
}

.ad-media-img {
  display: block;
  max-width: 168px;
  max-height: 112px;
  width: auto;
  height: auto;
  object-fit: contain;
}

.ad-copy {
  flex: 1 1 auto;
  min-width: 0;
}

.ad-headline {
  font-weight: 700;
  font-size: 0.94rem;
  line-height: 1.3;
  margin-bottom: 0.25rem;
}

.ad-description {
  font-size: 0.84rem;
  color: var(--muted);
  line-height: 1.45;
}

.ad-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.ad-meta {
  font-size: 0.74rem;
  color: var(--muted);
}

.ad-cta {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--accent);
  color: var(--accent-text);
  text-decoration: none;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0.4rem 0.85rem;
  transition: opacity 0.15s ease;
}

.ad-cta:hover {
  opacity: 0.88;
}

/* Footer / Composer */
.chat-footer {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 0.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom));
}

.chat-footer-inner {
  max-width: 780px;
  margin: 0 auto;
}

.error-banner {
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 0.84rem;
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.6rem;
}

.composer-form {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.composer-input {
  flex: 1 1 auto;
  font-family: var(--font-sans);
  font-size: 0.95rem;
  line-height: 1.45;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 0.62rem 0.85rem;
  resize: none;
  max-height: 140px;
  min-height: 44px;
  outline: none;
}

.composer-input:focus {
  border-color: var(--text);
}

.btn-send {
  flex: 0 0 auto;
  font-family: var(--font-sans);
  font-size: 0.9rem;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
  border: 1px solid var(--accent);
  padding: 0 1.15rem;
  height: 44px;
  cursor: pointer;
  transition: opacity 0.15s ease;
}

.btn-send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.composer-hint {
  margin-top: 0.4rem;
  font-size: 0.74rem;
  color: var(--muted);
  text-align: center;
}

.composer-hint kbd {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  border: 1px solid var(--border);
  padding: 0.05rem 0.3rem;
  background: var(--surface);
}

/* Mobile Responsive */
@media (max-width: 600px) {
  .welcome-card {
    margin-top: 1.25rem;
  }
  .bubble-user {
    max-width: 92%;
  }
  .ad-body {
    flex-direction: column;
  }
  .ad-media,
  .ad-media-img {
    max-width: 100%;
    max-height: 160px;
  }
  .composer-hint {
    display: none;
  }
}
