:root {
  --bg: #ffffff;
  --sidebar-bg: #f9f9f9;
  --border: #ececec;
  --border-strong: #e0e0e0;
  --ink: #1a1a1a;
  --ink-mid: #6b6b6b;
  --ink-soft: #a3a3a3;
  --hover: #f0f0f0;
  --accent: #1a1a1a;
  --radius: 12px;
  --sidebar-w: 260px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  display: flex;
  /* dvh so the iOS keyboard shrinking the viewport doesn't push the composer
     off-screen the way 100vh does. */
  height: 100dvh;
  overflow: hidden;
  /* Grey flash on every tap otherwise, which makes the whole thing feel cheap. */
  -webkit-tap-highlight-color: transparent;
}

svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

/* ---------- sidebar ---------- */

.sidebar {
  width: var(--sidebar-w);
  flex: none;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 14px;
}

.mark {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1.5px solid var(--ink);
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: -0.02em;
  flex: none;
}

.mark-lg {
  width: 44px;
  height: 44px;
  font-size: 15px;
  margin-bottom: 22px;
}

.brand-name {
  font-weight: 600;
  font-size: 15px;
}

.new-chat,
.chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}

.new-chat { font-weight: 500; }
.new-chat:hover,
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--hover); font-weight: 500; }

.chat-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  margin: 8px 0;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  color: var(--ink-soft);
}

.search input {
  border: 0;
  outline: none;
  background: transparent;
  font: inherit;
  /* 16px is a hard floor on any focusable input: iOS Safari auto-zooms the
     viewport when a focused control computes below it, which shifts the whole
     layout on tap. Style the box smaller if needed, never the text. */
  font-size: 16px;
  color: var(--ink);
  width: 100%;
  min-width: 0;
}

.search input::placeholder { color: var(--ink-soft); }

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0 -2px;
  padding: 0 2px;
}

.empty-note {
  color: var(--ink-soft);
  font-size: 13px;
  padding: 10px;
}

.sidebar-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px 0;
  border-top: 1px solid var(--border);
  margin-top: 8px;
  font-size: 14px;
}

.avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--ink);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 10px;
  font-weight: 700;
  flex: none;
}

/* ---------- main ---------- */

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex: none;
}

.topbar-title { font-weight: 600; font-size: 15px; }

.icon-btn {
  border: 0;
  background: transparent;
  padding: 8px;
  border-radius: 8px;
  color: var(--ink);
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn:hover { background: var(--hover); }

.scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  /* Stops a scroll at the top of the thread from triggering pull-to-refresh,
     which would discard an in-flight answer. */
  overscroll-behavior: contain;
}

.hero,
.thread {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 24px;
}

.hero {
  padding-top: min(16vh, 140px);
  padding-bottom: 40px;
}

.hero.hidden { display: none; }

.hero h1 {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0;
}

.hero .sub {
  font-size: 30px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--ink-soft);
  margin: 2px 0 34px;
}

.suggested-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-mid);
  margin: 0 0 12px;
}

.suggested-label svg { width: 14px; height: 14px; }

.cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 132px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fcfcfc;
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.card:hover { background: var(--hover); border-color: var(--border-strong); }

.card-title { font-size: 14px; font-weight: 600; }
.card-desc { font-size: 13px; color: var(--ink-mid); line-height: 1.4; }

.card-foot {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: space-between;
  font-size: 12px;
  color: var(--ink-soft);
}

.card-foot svg { width: 14px; height: 14px; }

/* ---------- messages ---------- */

.thread { padding-top: 24px; padding-bottom: 24px; }
.thread:empty { padding: 0; }

.msg { margin-bottom: 26px; }

.msg-user {
  display: flex;
  justify-content: flex-end;
}

.msg-user .bubble {
  background: #f4f4f4;
  border-radius: 18px;
  padding: 10px 16px;
  max-width: 78%;
  font-size: 15px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg-bot .bubble {
  font-size: 15px;
  line-height: 1.65;
  overflow-wrap: anywhere;
}

.msg-bot .bubble p { margin: 0 0 12px; }
.msg-bot .bubble p:last-child { margin-bottom: 0; }
.msg-bot .bubble ul,
.msg-bot .bubble ol { margin: 0 0 12px; padding-left: 22px; }
.msg-bot .bubble li { margin-bottom: 5px; }
.msg-bot .bubble h3 { font-size: 16px; margin: 16px 0 8px; }
.msg-bot .bubble h4 { font-size: 15px; margin: 14px 0 6px; }
.msg-bot .bubble code {
  background: #f4f4f4;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 13px;
}

.sources {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}

.source-chip {
  font: inherit;
  font-size: 12px;
  color: var(--ink-mid);
  background: #f6f6f6;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 11px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
  /* Comfortable thumb target without making the chips look chunky. */
  min-height: 30px;
}

.source-chip:hover {
  background: #ededed;
  border-color: var(--border-strong);
}

.source-chip[aria-expanded='true'] {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.source-chip:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.source-panel {
  margin-top: 10px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--ink);
  border-radius: 8px;
  background: #fbfbfb;
}

.source-panel-head {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 600;
}

.source-panel-body {
  margin: 0;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  white-space: pre-wrap;
}

.source-panel-foot {
  margin: 10px 0 0;
  font-size: 12px;
  color: var(--ink-soft);
}

.error-note {
  font-size: 14px;
  color: #a33;
  background: #fdf3f3;
  border: 1px solid #f2dede;
  border-radius: 8px;
  padding: 10px 12px;
}

.cursor {
  display: inline-block;
  width: 7px;
  height: 15px;
  background: var(--ink);
  vertical-align: -2px;
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink { to { visibility: hidden; } }

/* ---------- composer ---------- */

.composer-wrap {
  flex: none;
  padding: 8px 24px calc(12px + env(safe-area-inset-bottom));
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  border: 1px solid var(--border-strong);
  border-radius: 26px;
  padding: 8px 8px 8px 18px;
  background: var(--bg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.composer:focus-within { border-color: #cfcfcf; }

.composer textarea {
  flex: 1;
  border: 0;
  outline: none;
  resize: none;
  font: inherit;
  /* See .search input — below 16px iOS zooms on focus. */
  font-size: 16px;
  line-height: 1.5;
  padding: 6px 0;
  max-height: 160px;
  background: transparent;
  color: var(--ink);
}

.composer textarea::placeholder { color: var(--ink-soft); }

.send {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}

.send:disabled { background: #d6d6d6; cursor: default; }

.disclaimer {
  text-align: center;
  font-size: 11px;
  color: var(--ink-soft);
  margin: 8px 0 0;
  line-height: 1.4;
}

/* ---------- mobile ---------- */

.scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.32);
  border: 0;
  padding: 0;
  z-index: 20;
}

@media (max-width: 820px) {
  .topbar { display: flex; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    /* On a 320px phone a fixed 260px drawer leaves only 60px of scrim, which is
       a cramped tap target for dismissing it. 76vw yields ~77px there and
       changes nothing at 390px and up, where 260px already wins. */
    width: min(var(--sidebar-w), 76vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    padding-top: calc(14px + env(safe-area-inset-top));
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }

  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open .scrim { display: block; }

  .hero,
  .thread,
  .composer-wrap { padding-left: 16px; padding-right: 16px; }

  .hero { padding-top: 40px; }
  .hero h1 { font-size: 26px; }
  .hero .sub { font-size: 24px; margin-bottom: 26px; }

  .cards { grid-template-columns: 1fr; }
  .card { min-height: 0; }
  .card-foot { margin-top: 10px; }

  .msg-user .bubble { max-width: 88%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
  .scroll { scroll-behavior: auto; }
}
