:root {
  --bg: #0c0c0e;
  --surface: #161618;
  --surface-2: #1f1f23;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.14);
  --fg: #f5f5f7;
  --fg-mute: #a1a1a8;
  --fg-soft: #6e6e76;
  --accent: #a78bfa;
  --accent-strong: #b8a3ff;
  --accent-on: #18102b;
  --user-bg: #232328;
  --user-fg: #f5f5f7;
  --danger: #f87171;
  --warn: #fbbf24;
  --ok: #34d399;
  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --col-width: 760px;
  --shadow-1: 0 1px 0 rgba(255,255,255,0.04) inset, 0 8px 32px rgba(0,0,0,0.35);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI Variable", "Segoe UI",
    "Inter", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
body {
  display: flex;
  flex-direction: column;
  /* 100vh on mobile browsers = the LARGEST viewport (URL bar hidden), so
     when the URL bar is shown the page extends below the visible area
     and the composer falls off-screen. dvh tracks the actual visible
     viewport. vh stays as a fallback for older browsers. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Header ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(12, 12, 14, 0.78);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: var(--col-width);
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.brand-mark {
  width: 18px; height: 18px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 30%, var(--accent-strong), var(--accent) 60%, #5b3fbd 100%);
  box-shadow: 0 0 16px rgba(167, 139, 250, 0.45);
}
.brand-name {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}
.status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--fg-mute);
  padding: 4px 10px 4px 9px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  user-select: none;
  cursor: default;
}
.status .dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 0 0 currentColor;
  flex: none;
}
.status.ok    { color: var(--ok); border-color: rgba(52, 211, 153, 0.25); }
.status.ok .dot { animation: ok-pulse 2.4s ease-in-out infinite; }
.status.busy  { color: var(--warn); border-color: rgba(251, 191, 36, 0.30); }
.status.busy .dot { animation: busy-pulse 1.2s ease-in-out infinite; }
.status.err   { color: var(--danger); border-color: rgba(248, 113, 113, 0.35); }
.status.warn  { color: var(--warn); border-color: rgba(251, 191, 36, 0.30); }

@keyframes ok-pulse {
  0%, 100% { box-shadow: 0 0 0 0 currentColor; opacity: 0.9; }
  50%      { box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.18); opacity: 1; }
}
@keyframes busy-pulse {
  0%, 100% { opacity: 0.55; }
  50%      { opacity: 1; }
}

.btn-ghost {
  appearance: none;
  background: transparent;
  color: var(--fg-mute);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 14px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: color 120ms ease, border-color 120ms ease, background 120ms ease;
}
.btn-ghost:hover:not(:disabled) {
  color: var(--fg);
  border-color: var(--border-strong);
  background: var(--surface);
}
.btn-ghost:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Scroll area ---------- */
.scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-2) transparent;
}
.scroll::-webkit-scrollbar { width: 10px; }
.scroll::-webkit-scrollbar-thumb { background: var(--surface-2); border-radius: 10px; border: 2px solid var(--bg); }
.scroll::-webkit-scrollbar-thumb:hover { background: #2c2c33; }

.column {
  max-width: var(--col-width);
  margin: 0 auto;
  padding: 28px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* ---------- Empty state ---------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: clamp(48px, 12vh, 120px) 0 32px;
  gap: 14px;
  animation: fade-in 360ms ease;
}
.empty-title {
  margin: 0;
  font-size: clamp(34px, 5.5vw, 44px);
  font-weight: 600;
  letter-spacing: -0.01em;
  background: linear-gradient(180deg, #f5f5f7 0%, #c7c7d0 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.empty-sub {
  margin: 0;
  max-width: 540px;
  color: var(--fg-mute);
  font-size: 15px;
  line-height: 1.65;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 18px;
  max-width: 580px;
}
.chip {
  appearance: none;
  background: var(--surface);
  color: var(--fg-mute);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: all 120ms ease;
}
.chip:hover {
  color: var(--fg);
  background: var(--surface-2);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}

/* ---------- Messages ---------- */
.messages {
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.msg {
  animation: fade-up 220ms ease both;
  max-width: 100%;
}
.msg-user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-fg);
  padding: 10px 16px;
  border-radius: var(--radius);
  border-bottom-right-radius: 6px;
  border: 1px solid var(--border);
  max-width: 82%;
  white-space: pre-wrap;
  word-wrap: break-word;
}
.msg-assistant {
  align-self: stretch;
  color: var(--fg);
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 15.5px;
  line-height: 1.7;
  /* Prevent mobile browsers from intercepting double-tap as a zoom
     gesture (would delay or swallow our second click event). */
  touch-action: manipulation;
}
.msg-assistant strong { color: var(--fg); font-weight: 600; }
.msg-assistant em { color: var(--fg); font-style: italic; }
.msg-assistant code {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.msg-system {
  align-self: center;
  font-size: 12px;
  color: var(--fg-soft);
  padding: 4px 12px;
  border: 1px dashed var(--border);
  border-radius: 999px;
  background: transparent;
}

/* Loading indicator: three pulsing dots inline */
.dots {
  display: inline-flex;
  gap: 4px;
  vertical-align: middle;
  align-items: center;
  height: 1em;
}
.dots span {
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--fg-mute);
  opacity: 0.4;
  animation: dot-pulse 1.2s ease-in-out infinite;
}
.dots span:nth-child(2) { animation-delay: 0.15s; }
.dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes dot-pulse {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}
@keyframes fade-up {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ---------- Composer ---------- */
.composer-wrap {
  position: sticky;
  bottom: 0;
  z-index: 10;
  /* Respect iOS home-indicator safe area so the composer isn't visually
     overlapping the gesture bar at the bottom of the screen. */
  padding: 14px 24px calc(18px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(12,12,14,0) 0%, rgba(12,12,14,0.95) 30%, var(--bg) 60%);
}
.composer {
  max-width: var(--col-width);
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
  transition: border-color 120ms ease;
}
.composer:focus-within {
  border-color: rgba(167, 139, 250, 0.45);
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.12), var(--shadow-1);
}
#input {
  flex: 1;
  background: transparent;
  color: var(--fg);
  border: none;
  outline: none;
  padding: 10px 0;
  font: inherit;
  font-size: 16px;
  line-height: 1.5;
  resize: none;
  min-height: 24px;
  max-height: 200px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-2) transparent;
}
#input::placeholder { color: var(--fg-soft); }
.btn-send {
  appearance: none;
  flex-shrink: 0;
  width: 36px; height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: var(--accent-on);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  transition: background 120ms ease, transform 120ms ease, opacity 120ms ease;
  /* Suppress native long-press behaviors so the sneaky trigger isn't
     hijacked by iOS/Android context menus, text selection, callouts,
     or tap-highlight flashes. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn-send:hover:not(:disabled) {
  background: var(--accent-strong);
}
.btn-send:active:not(:disabled) {
  transform: scale(0.94);
}
.btn-send:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}
.composer-hint {
  max-width: var(--col-width);
  margin: 8px auto 0;
  font-size: 11px;
  color: var(--fg-soft);
  text-align: center;
}

@media (max-width: 640px) {
  .topbar-inner, .column, .composer, .composer-hint { padding-left: 16px; padding-right: 16px; }
  .column { padding-top: 18px; gap: 18px; }
  .msg-user { max-width: 92%; }
}
