:root {
  --cream: #faf6ef;
  --cream-2: #f3ebdd;
  --panel: #ffffff;
  --ink: #1f2430;
  --ink-soft: #5a6172;
  --teal: #0f766e;
  --teal-deep: #0c5d57;
  --teal-soft: #99dfd6;
  --teal-tint: #e8f5f2;
  --line: #e7ddcb;
  --line-soft: #efe7d8;
  --danger: #b4453a;
  --amber: #c9892f;
  --sidebar-w: 272px;
  --radius: 16px;
  --shadow-card: 0 1px 2px rgba(31, 36, 48, 0.05), 0 8px 28px -18px rgba(31, 36, 48, 0.35);
  --shadow-pop: 0 24px 60px -28px rgba(31, 36, 48, 0.45);
}

* { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  color: var(--ink);
  background: var(--cream);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.muted { color: var(--ink-soft); }
.is-hidden { display: none !important; }

/* ── App shell ───────────────────────────────────────────── */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
  overflow: hidden;
  transition: grid-template-columns 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.app.is-collapsed { grid-template-columns: 0 1fr; }

/* ── Sidebar ─────────────────────────────────────────────── */
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: linear-gradient(180deg, #f6efe2, #f1e8d8);
  border-right: 1px solid var(--line);
  overflow: hidden;
}
.app.is-collapsed .sidebar { border-right-color: transparent; }

.sidebar__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 14px 12px 18px;
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand__mark {
  font-size: 1.1rem;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  background: var(--teal-tint);
  border: 1px solid var(--teal-soft);
  border-radius: 9px;
}
.brand__name {
  font-family: "Space Grotesk", sans-serif;
  font-weight: 700;
  font-size: 1.02rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.icon-btn {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.icon-btn:hover { background: rgba(31, 36, 48, 0.07); color: var(--ink); }

.new-chat {
  margin: 4px 14px 14px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit;
  font-weight: 600;
  font-size: 0.92rem;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--teal);
  background: var(--teal);
  color: #fff;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 8px 18px -12px rgba(15, 118, 110, 0.9);
  transition: background 0.16s ease, transform 0.06s ease;
}
.new-chat:hover { background: var(--teal-deep); }
.new-chat:active { transform: translateY(1px); }

/* Conversation history list */
.conversations {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.conv-group {
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
  padding: 14px 10px 4px;
}
.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--ink);
  font-size: 0.9rem;
  border: 1px solid transparent;
  transition: background 0.13s ease, border-color 0.13s ease;
}
.conv-item:hover { background: rgba(31, 36, 48, 0.05); }
.conv-item.is-active {
  background: var(--panel);
  border-color: var(--line);
  box-shadow: 0 1px 2px rgba(31, 36, 48, 0.04);
}
.conv-item__title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-item__del {
  flex: 0 0 auto;
  display: grid; place-items: center;
  width: 24px; height: 24px;
  border: none; border-radius: 7px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.13s ease, background 0.13s ease, color 0.13s ease;
}
.conv-item:hover .conv-item__del,
.conv-item.is-active .conv-item__del { opacity: 1; }
.conv-item__del:hover { background: rgba(180, 69, 58, 0.12); color: var(--danger); }
.conv-empty { padding: 18px 12px; font-size: 0.85rem; color: var(--ink-soft); text-align: center; }

/* Sidebar footer: engine status + local badge */
.sidebar__foot {
  border-top: 1px solid var(--line-soft);
  padding: 12px 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.engine-mini { display: flex; align-items: center; gap: 10px; }
.engine-mini__text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.engine-mini__text strong {
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.84rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.engine-mini__text .muted { font-size: 0.76rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.local-badge {
  align-self: flex-start;
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.74rem;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 99px;
  background: var(--teal-soft);
  color: var(--teal-deep);
  white-space: nowrap;
}
.local-badge.is-offline { background: #f3d9a8; color: #8a5a12; }

.dot {
  width: 11px; height: 11px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: var(--ink-soft);
}
.dot--idle { background: var(--ink-soft); }
.dot--loading { background: var(--amber); animation: pulse 1.2s ease-in-out infinite; }
.dot--ready { background: var(--teal); box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.15); }
.dot--error { background: var(--danger); }
@keyframes pulse { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(0.65); opacity: 0.5; } }

.progress { height: 6px; border-radius: 99px; background: var(--cream-2); overflow: hidden; }
.progress__bar {
  height: 100%; width: 0;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--teal-soft), var(--teal));
  transition: width 0.25s ease;
}

/* ── Main column ─────────────────────────────────────────── */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100dvh;
  background: var(--cream);
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line-soft);
  background: rgba(250, 246, 239, 0.82);
  backdrop-filter: blur(10px);
  z-index: 5;
}
.topbar #menuBtn { display: none; }
.topbar__title { display: flex; flex-direction: column; gap: 1px; min-width: 0; flex: 1; }
.topbar__title strong { font-family: "Space Grotesk", sans-serif; font-size: 0.98rem; }
.topbar__meta { font-size: 0.78rem; }
.topbar__pill {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-family: "Space Grotesk", sans-serif;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  padding: 5px 11px;
  border-radius: 99px;
  background: var(--teal-tint);
  color: var(--teal-deep);
  border: 1px solid var(--teal-soft);
}
.topbar__pill-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--teal);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.16);
}
.topbar__pill.is-offline {
  background: #f7ecd4;
  color: #8a5a12;
  border-color: #f0d9a8;
}
.topbar__pill.is-offline .topbar__pill-dot {
  background: var(--amber);
  box-shadow: 0 0 0 3px rgba(201, 137, 47, 0.18);
}

/* Scroll region holds the centered conversation column */
.chat-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
}
.messages {
  max-width: 760px;
  margin: 0 auto;
  padding: 28px 22px 8px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

/* ── Welcome / empty state ───────────────────────────────── */
.welcome {
  text-align: center;
  padding: clamp(24px, 7vh, 70px) 8px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.welcome__mark {
  font-size: 1.7rem;
  width: 60px; height: 60px;
  display: grid; place-items: center;
  background: var(--teal-tint);
  border: 1px solid var(--teal-soft);
  border-radius: 18px;
  margin-bottom: 18px;
  box-shadow: var(--shadow-card);
}
.welcome__title {
  font-family: "Space Grotesk", sans-serif;
  font-size: clamp(1.7rem, 4.5vw, 2.3rem);
  letter-spacing: -0.02em;
  margin: 0 0 10px;
}
.welcome__sub { margin: 0 auto 26px; max-width: 46ch; color: var(--ink-soft); line-height: 1.55; }

.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  width: 100%;
  max-width: 560px;
}
.suggestion {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  font: inherit;
  padding: 14px 15px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--shadow-card);
  transition: border-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}
.suggestion:hover { border-color: var(--teal-soft); transform: translateY(-2px); }
.suggestion:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.suggestion__icon { font-size: 1.25rem; flex: 0 0 auto; }
.suggestion__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; font-size: 0.86rem; color: var(--ink-soft); }
.suggestion__text strong { color: var(--ink); font-weight: 600; font-size: 0.92rem; }

/* ── Messages ────────────────────────────────────────────── */
.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg--user { flex-direction: row-reverse; }

.avatar {
  flex: 0 0 auto;
  width: 32px; height: 32px;
  border-radius: 9px;
  display: grid; place-items: center;
  font-size: 0.95rem;
  font-weight: 600;
  user-select: none;
}
.msg--assistant .avatar { background: var(--teal-tint); border: 1px solid var(--teal-soft); }
.msg--user .avatar { background: var(--teal); color: #fff; font-family: "Space Grotesk", sans-serif; font-size: 0.82rem; }

.bubble {
  max-width: 100%;
  padding: 12px 16px;
  border-radius: 16px;
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg--user .bubble {
  background: var(--teal);
  color: #fff;
  border-top-right-radius: 5px;
  white-space: pre-wrap;
}
.msg--assistant .bubble {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line-soft);
  border-top-left-radius: 5px;
  box-shadow: var(--shadow-card);
}

/* Assistant message column + actions (Copy) */
.msg__col { display: flex; flex-direction: column; gap: 5px; min-width: 0; max-width: 100%; }
.msg-actions { display: flex; gap: 4px; padding-left: 4px; }
.msg__col:has(.bubble.is-streaming) .msg-actions { display: none; }
.msg-action {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--ink-soft);
  background: transparent;
  border: none;
  border-radius: 7px;
  padding: 4px 8px;
  cursor: pointer;
  transition: background 0.13s ease, color 0.13s ease;
}
.msg-action:hover { background: rgba(31, 36, 48, 0.07); color: var(--ink); }
.msg-action.is-copied { color: var(--teal); }

/* Streaming caret */
.bubble.is-streaming::after {
  content: "▍";
  color: var(--teal);
  margin-left: 1px;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Markdown inside assistant bubbles */
.bubble p { margin: 0 0 10px; }
.bubble > :last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { font-family: "Space Grotesk", sans-serif; line-height: 1.3; margin: 16px 0 8px; }
.bubble h1 { font-size: 1.25rem; }
.bubble h2 { font-size: 1.12rem; }
.bubble h3 { font-size: 1rem; }
.bubble ul, .bubble ol { margin: 8px 0 10px; padding-left: 22px; }
.bubble li { margin: 5px 0; }
.bubble li::marker { color: var(--teal); }
.bubble a { color: var(--teal-deep); text-decoration: underline; text-underline-offset: 2px; }
.bubble strong { font-weight: 600; }
.bubble code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.88em;
  background: var(--cream-2);
  padding: 1.5px 5px;
  border-radius: 5px;
}
.bubble pre {
  background: #1f2430;
  color: #f3ebdd;
  padding: 14px 16px;
  border-radius: 12px;
  overflow-x: auto;
  margin: 10px 0;
  line-height: 1.5;
}
.bubble pre code { background: transparent; padding: 0; font-size: 0.84em; color: inherit; }
.bubble blockquote {
  margin: 10px 0;
  padding: 4px 14px;
  border-left: 3px solid var(--teal-soft);
  color: var(--ink-soft);
}
.bubble table { border-collapse: collapse; margin: 10px 0; font-size: 0.9em; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.bubble th { background: var(--cream-2); }
.bubble hr { border: none; border-top: 1px solid var(--line); margin: 14px 0; }

/* ── Composer ────────────────────────────────────────────── */
.composer-wrap {
  flex: 0 0 auto;
  padding: 4px 22px 14px;
  background: linear-gradient(180deg, rgba(250, 246, 239, 0), var(--cream) 32%);
}
.composer-wrap > * { max-width: 760px; margin-left: auto; margin-right: auto; }

.quick-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  padding: 8px 2px;
  justify-content: center;
}
.chip {
  font: inherit;
  font-size: 0.83rem;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 99px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}
.chip:hover:not(:disabled) { border-color: var(--teal); background: var(--teal-tint); color: var(--teal-deep); }
.chip:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Web-search opt-in ─────────────────────────────────────────────── */
.search-banner {
  font-size: 0.8rem;
  color: var(--teal-deep);
  background: var(--teal-tint);
  border: 1px solid var(--teal-soft);
  border-radius: 12px;
  padding: 8px 12px;
  margin-bottom: 8px;
  text-align: center;
}
.search-banner.is-hidden { display: none; }

.search-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
  padding-right: 4px;
}
.search-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.search-switch__track {
  position: relative;
  width: 34px; height: 20px;
  border-radius: 99px;
  background: #cdd6d4;
  transition: background 0.16s ease;
  flex: 0 0 auto;
}
.search-switch__track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.16s ease;
}
.search-switch input:checked + .search-switch__track { background: var(--teal); }
.search-switch input:checked + .search-switch__track::after { transform: translateX(14px); }
.search-switch input:focus-visible + .search-switch__track { outline: 2px solid var(--teal-soft); outline-offset: 2px; }
.search-switch__label {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--ink-soft);
  white-space: nowrap;
}
.search-switch input:checked ~ .search-switch__label { color: var(--teal-deep); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 26px;
  box-shadow: var(--shadow-pop);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.composer:focus-within { border-color: var(--teal-soft); box-shadow: 0 24px 60px -28px rgba(15, 118, 110, 0.5); }

#input {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  font: inherit;
  line-height: 1.55;
  color: var(--ink);
  padding: 9px 0;
  max-height: 220px;
  overflow-y: auto;
}
#input::placeholder { color: var(--ink-soft); }
#input:disabled { cursor: not-allowed; opacity: 0.7; }

.composer__actions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }

.circle-btn {
  display: grid; place-items: center;
  width: 40px; height: 40px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.16s ease, transform 0.06s ease, opacity 0.16s ease;
}
.circle-btn:active { transform: scale(0.94); }
.circle-btn--send { background: var(--teal); color: #fff; }
.circle-btn--send:hover:not(:disabled) { background: var(--teal-deep); }
.circle-btn--send:disabled { background: #cdd6d4; color: #fff; cursor: not-allowed; }
.circle-btn--stop { background: var(--cream-2); color: var(--ink); }
.circle-btn--stop:hover { background: #e6dcc8; }

.composer__hint {
  text-align: center;
  font-size: 0.78rem;
  margin: 9px 0 0;
}

/* ── Scrim (mobile sidebar overlay) ──────────────────────── */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(31, 36, 48, 0.4);
  backdrop-filter: blur(2px);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.scrim.is-shown { opacity: 1; }

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .app.is-collapsed { grid-template-columns: 1fr; }
  .topbar #menuBtn { display: grid; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 280px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: var(--shadow-pop);
  }
  .app.nav-open .sidebar { transform: translateX(0); }
  .sidebar__collapse { display: none; }
  .suggestions { grid-template-columns: 1fr; }
}
