:root {
  --bg: #f6f7f9; --fg: #1d2330; --muted: #677085; --card: #fff; --line: #dfe3ea;
  --accent: #3b5bdb; --accent-fg: #fff; --user: #e7ecfb; --err: #c92a2a; --tool: #eef1f5;
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14171c; --fg: #e4e7ec; --muted: #98a1b3; --card: #1d2128; --line: #2e343e;
          --accent: #5c7cfa; --user: #253049; --err: #ff6b6b; --tool: #23282f; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
#app { height: 100%; max-width: 820px; margin: 0 auto; }
[hidden] { display: none !important; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 24px;
        margin: 10vh 16px 0; max-width: 420px; margin-left: auto; margin-right: auto; }
.card h1 { margin: 0 0 4px; font-size: 1.4rem; }
.muted { color: var(--muted); margin-top: 0; }
label { display: block; margin: 14px 0; font-weight: 600; font-size: .9rem; }
input, select, textarea { display: block; width: 100%; margin-top: 4px; padding: 10px 12px; font: inherit;
  color: var(--fg); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
button { font: inherit; padding: 10px 16px; border: 0; border-radius: 8px; background: var(--accent);
         color: var(--accent-fg); cursor: pointer; font-weight: 600; }
button:disabled { opacity: .5; cursor: default; }
button.small { padding: 6px 10px; font-size: .85rem; background: transparent; color: var(--fg);
               border: 1px solid var(--line); font-weight: 500; }
button.link { background: none; color: var(--muted); padding: 8px 0; font-weight: 400; text-decoration: underline; }
.error { color: var(--err); min-height: 1.5em; margin: 8px 0 0; }

#view-chat { display: flex; flex-direction: column; height: 100%; }
header { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: space-between;
         padding: 10px 14px; border-bottom: 1px solid var(--line); background: var(--card); }
header .actions { display: flex; gap: 6px; }
#messages { flex: 1; overflow-y: auto; padding: 16px 14px; }
.msg { margin: 0 0 16px; max-width: 92%; }
.msg.user { margin-left: auto; background: var(--user); padding: 10px 14px; border-radius: 14px 14px 4px 14px;
            white-space: pre-wrap; width: fit-content; }
.msg.assistant { background: var(--card); border: 1px solid var(--line); padding: 10px 14px;
                 border-radius: 14px 14px 14px 4px; }
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg .err { color: var(--err); }
.msg .notice { color: var(--muted); font-style: italic; }
.tool { display: inline-block; font-size: .8rem; color: var(--muted); background: var(--tool);
        border-radius: 999px; padding: 2px 10px; margin: 2px 4px 2px 0; }
.tool::before { content: "⚙ "; }
.tool.running::after { content: " …"; }
.tool.failed { color: var(--err); }
.dots::after { content: "…"; animation: dots 1.2s steps(4) infinite; display: inline-block; width: 1.2em;
               overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0 } to { width: 1.2em } }
.msg table { border-collapse: collapse; margin: 8px 0; font-size: .9rem; display: block; overflow-x: auto; }
.msg th, .msg td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; }
.msg code { background: var(--tool); padding: 1px 4px; border-radius: 4px; font-size: .88em; }
.msg pre { background: var(--tool); padding: 8px; border-radius: 6px; overflow-x: auto; }
.msg pre code { background: none; padding: 0; }
.msg h1, .msg h2, .msg h3 { font-size: 1.05rem; margin: 12px 0 4px; }
.msg ul, .msg ol { padding-left: 1.4em; margin: 6px 0; }
.msg p { margin: 6px 0; }
.empty { color: var(--muted); text-align: center; margin-top: 15vh; }

#composer { display: flex; gap: 8px; padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
            border-top: 1px solid var(--line); background: var(--card); align-items: flex-end; }
#composer textarea { margin: 0; resize: none; max-height: 40vh; }
#btn-stop { background: var(--err); }
