@font-face {
  font-family: "Fraunces";
  src: url("/static/fonts/fraunces-latin.woff2") format("woff2");
  font-weight: 300 700;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Newsreader";
  src: url("/static/fonts/newsreader-latin.woff2") format("woff2");
  font-weight: 300 600;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Mono";
  src: url("/static/fonts/plex-mono-400.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "IBM Plex Mono";
  src: url("/static/fonts/plex-mono-500.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --ink: #1f2933;
  --muted: #68737d;
  --faint: #9aa3ab;
  --line: #e4e6e6;
  --line-strong: #cdd1d2;
  --accent: #2563eb;
  --accent-soft: #eef4ff;
  --user: #1f2937;
  --code-bg: #1f2328;
  --font-display: "Fraunces", Georgia, "Songti SC", "STSong", serif;
  --font-body: "Newsreader", Georgia, "Songti SC", "Noto Serif SC", "SimSun", serif;
  --font-ui: "IBM Plex Mono", "Cascadia Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection { background: #dbeafe; color: var(--ink); }
button, textarea, select, input { font: inherit; }
button { color: inherit; }

.app {
  height: 100vh;
  height: 100dvh;
  width: min(920px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: 0 24px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 2px 14px;
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: baseline; gap: 10px; }
h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 28px;
  font-weight: 500;
  letter-spacing: -0.045em;
  line-height: 1;
}
.badge {
  font-family: var(--font-ui);
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 3px 7px;
  border: 1px solid var(--line);
  border-radius: 99px;
  white-space: nowrap;
}

.header-actions { display: flex; align-items: center; gap: 14px; }
.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-ui);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--muted);
  white-space: nowrap;
}
.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #16a34a;
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.12);
}
.status.is-busy .dot { background: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.status.is-offline .dot { background: #dc2626; box-shadow: none; }

.plain-button {
  border: 0;
  background: none;
  padding: 0;
  color: var(--muted);
  font-family: var(--font-ui);
  font-size: 10px;
  letter-spacing: 0.05em;
  cursor: pointer;
}
.plain-button:hover { color: var(--accent); }

.chat { min-height: 0; overflow: hidden; }
.messages {
  height: 100%;
  overflow-y: auto;
  padding: 24px 2px 32px;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}
.messages::-webkit-scrollbar { width: 5px; }
.messages::-webkit-scrollbar-thumb { background: var(--line-strong); }

/* welcome */
.welcome { padding: 10vh 0 30px; max-width: 600px; }
.welcome h2 {
  margin: 0 0 10px;
  font-family: var(--font-display);
  font-size: clamp(30px, 4vw, 40px);
  font-weight: 400;
  letter-spacing: -0.04em;
  line-height: 1.05;
}
.welcome p {
  margin: 0 0 24px;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.55;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 99px;
  padding: 8px 13px;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 14px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}
.chip:hover { border-color: var(--accent); background: var(--accent-soft); }

/* messages */
.message {
  display: flex;
  gap: 10px;
  margin-bottom: 22px;
  animation: fade 0.25s ease both;
}
.message.user { justify-content: flex-end; }
.avatar {
  width: 26px;
  height: 26px;
  flex: 0 0 26px;
  margin-top: 2px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  background: #e8eeea;
  color: #31533d;
  font-family: var(--font-ui);
  font-size: 10px;
  font-weight: 500;
}
.bubble {
  max-width: min(78%, 680px);
  padding: 11px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.message.user .bubble {
  background: var(--user);
  border-color: var(--user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.message.assistant .bubble { border-bottom-left-radius: 4px; }
.content { font-size: 16px; white-space: pre-wrap; }
.message.user .content { font-size: 15px; }

.trace {
  margin: 0 0 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fafafa;
  font-family: var(--font-ui);
  font-size: 11px;
  line-height: 1.55;
  color: var(--muted);
}
.trace summary {
  padding: 6px 8px;
  cursor: pointer;
  list-style: none;
  color: var(--faint);
  font-size: 10px;
  letter-spacing: 0.05em;
  user-select: none;
}
.trace summary::-webkit-details-marker { display: none; }
.trace summary::before { content: "+"; display: inline-block; width: 12px; }
.trace[open] summary::before { content: "−"; }
.trace-body { padding: 0 8px 7px 20px; white-space: pre-wrap; }
.hide-traces .trace { display: none; }

.answer-text { min-height: 1.2em; }
.answer-text.streaming::after {
  content: "▍";
  margin-left: 1px;
  color: var(--accent);
  animation: blink 0.8s steps(2, start) infinite;
}
.message-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  color: var(--faint);
  font-family: var(--font-ui);
  font-size: 9px;
  letter-spacing: 0.05em;
  opacity: 0;
}
.message.is-done .message-meta { opacity: 1; }
.copy-button {
  border: 0;
  background: none;
  padding: 0;
  color: var(--muted);
  font-family: var(--font-ui);
  font-size: 9px;
  letter-spacing: 0.05em;
  cursor: pointer;
}
.copy-button:hover { color: var(--accent); }
.error {
  margin-top: 6px;
  color: #b91c1c;
  font-family: var(--font-ui);
  font-size: 11px;
  line-height: 1.5;
}

/* markdown */
.content.markdown { white-space: normal; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p { margin: 0 0 0.7em; }
.markdown h1, .markdown h2, .markdown h3 {
  margin: 1em 0 0.35em;
  font-family: var(--font-display);
  font-weight: 500;
  line-height: 1.15;
  letter-spacing: -0.025em;
}
.markdown h1 { font-size: 1.7rem; }
.markdown h2 { font-size: 1.4rem; }
.markdown h3 { font-size: 1.15rem; }
.markdown ul, .markdown ol { margin: 0 0 0.8em; padding-left: 1.25em; }
.markdown li { margin: 0.15em 0; }
.markdown li::marker { color: var(--accent); }
.markdown blockquote {
  margin: 0.8em 0;
  padding: 0.1em 0 0.1em 12px;
  border-left: 2px solid var(--line-strong);
  color: var(--muted);
}
.markdown a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.markdown code {
  font-family: var(--font-ui);
  font-size: 0.8em;
  padding: 0.1em 0.3em;
  border-radius: 3px;
  background: #f0f2f3;
  color: #334155;
}
.markdown pre {
  position: relative;
  margin: 0.8em 0;
  padding: 13px 12px 11px;
  overflow-x: auto;
  border-radius: 8px;
  background: var(--code-bg);
  color: #e6edf3;
  font-family: var(--font-ui);
  font-size: 12px;
  line-height: 1.6;
}
.markdown pre[data-lang] { padding-top: 28px; }
.markdown pre[data-lang]::before {
  content: attr(data-lang);
  position: absolute;
  top: 7px;
  left: 12px;
  color: #9ca3af;
  font-family: var(--font-ui);
  font-size: 9px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.markdown pre code { padding: 0; background: transparent; color: inherit; font-size: inherit; }
.code-copy {
  position: absolute;
  top: 6px;
  right: 7px;
  border: 1px solid #4b5563;
  border-radius: 4px;
  background: transparent;
  color: #9ca3af;
  padding: 2px 5px;
  font-family: var(--font-ui);
  font-size: 8px;
  cursor: pointer;
}
.code-copy:hover { color: #fff; border-color: #9ca3af; }
.markdown table {
  display: block;
  width: 100%;
  overflow-x: auto;
  margin: 0.8em 0;
  border-collapse: collapse;
  font-size: 13px;
}
.markdown th, .markdown td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
}
.markdown th { font-family: var(--font-ui); font-size: 10px; color: var(--muted); }
.markdown hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }

/* composer */
.composer-wrap { border-top: 1px solid var(--line); padding: 12px 0 10px; background: var(--bg); }
.composer { display: flex; flex-direction: column; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
#prompt {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  padding: 10px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  outline: none;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.45;
  scrollbar-width: none;
}
#prompt::-webkit-scrollbar { display: none; }
#prompt::placeholder { color: var(--faint); }
#prompt:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.08); }
.send-button, .stop-button {
  border: 0;
  border-radius: 9px;
  padding: 10px 15px;
  font-family: var(--font-ui);
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
}
.send-button { background: var(--ink); color: #fff; }
.send-button:hover { background: var(--accent); }
.send-button:disabled { opacity: 0.4; cursor: not-allowed; }
.stop-button { background: #fee2e2; color: #991b1b; }
.composer-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 2px 0;
  color: var(--faint);
  font-family: var(--font-ui);
  font-size: 9px;
}
.settings { margin-top: 8px; font-family: var(--font-ui); font-size: 10px; color: var(--muted); }
.settings summary { cursor: pointer; width: fit-content; padding: 3px 0; }
.settings-row { display: flex; flex-wrap: wrap; gap: 18px; padding: 9px 2px 4px; align-items: flex-end; }
.settings-row label { display: flex; flex-direction: column; gap: 5px; }
.settings-row label span { display: flex; justify-content: space-between; gap: 8px; }
.settings-row b { font-weight: 500; color: var(--ink); }
.settings-row input[type="range"] { width: 130px; accent-color: var(--accent); }
.settings-row select {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 6px;
  background: var(--surface);
  font-family: var(--font-ui);
  font-size: 11px;
}
.settings-row .check { flex-direction: row; align-items: center; gap: 6px; padding-bottom: 4px; }
.settings-row .check input { accent-color: var(--accent); }
.settings-note { margin: 3px 2px 0; color: var(--faint); font-size: 9px; }

@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes blink { 50% { opacity: 0; } }

@media (max-width: 640px) {
  .app { padding: 0 12px; }
  .header { padding: 13px 0 11px; }
  h1 { font-size: 24px; }
  .badge { font-size: 9px; }
  .status { display: none; }
  .messages { padding: 16px 0 22px; }
  .welcome { padding-top: 8vh; }
  .welcome h2 { font-size: 30px; }
  .bubble { max-width: 86%; }
  .content { font-size: 15px; }
  .composer-wrap { padding: 10px 0 8px; }
  .composer-meta span:first-child { display: none; }
  .settings-row { gap: 12px; }
  .settings-row input[type="range"] { width: 110px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
