:root {
  --bg: #faf8f4;
  --surface: #ffffff;
  --surface-2: #f3efe8;
  --line: #e7e1d7;
  --text: #2a211d;
  --muted: #7a6e66;
  --accent: #e2643b;
  --accent-strong: #c24e28;
  --accent-soft: #fde9df;
  --ok: #2f9e6b;
  --warn: #d99a1e;
  --err: #c8392b;
  --user-bubble: #efe9df;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(42, 33, 29, 0.05), 0 6px 24px rgba(42, 33, 29, 0.06);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 var(--font);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
code { font-family: var(--mono); font-size: 0.9em; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: rgba(250, 248, 244, 0.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 8px; }
.brand-name { font-family: var(--serif); font-size: 19px; font-weight: 600; letter-spacing: -0.01em; }
.brand-tag {
  font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--accent-strong); background: var(--accent-soft); padding: 2px 7px; border-radius: 999px;
}
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 10px; margin-left: 8px; }
.tab {
  border: 0; background: transparent; padding: 5px 14px; border-radius: 8px; cursor: pointer; color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px; transition: background-color .15s, color .15s;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.tab .count {
  font-size: 11px; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--line); color: var(--text);
  font-variant-numeric: tabular-nums;
}
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.mcp-status {
  display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted);
  padding: 5px 11px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface);
  max-width: 300px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mcp-status .dot { width: 8px; height: 8px; border-radius: 50%; background: #bfb6ad; flex: none; }
.mcp-status[data-state="connected"] .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(47,158,107,.18); }
.mcp-status[data-state="connected"] { color: var(--text); }
.mcp-status[data-state="pending"] .dot { background: var(--warn); animation: pulse 1.2s infinite; }
.mcp-status[data-state="error"] .dot { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }

.btn {
  border: 1px solid transparent; border-radius: 10px; padding: 7px 14px; cursor: pointer; font-weight: 500;
  transition: background-color .15s, border-color .15s, transform .1s;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover:not(:disabled) { background: var(--accent-strong); }
.btn-ghost { background: var(--surface); border-color: var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-danger { background: #fff; border-color: #efc7c0; color: var(--err); }
.btn-danger:hover { background: #fdf0ed; }
.icon-btn {
  width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; color: var(--muted);
}
.icon-btn:hover { color: var(--text); background: var(--surface-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.banner {
  margin: 10px auto 0; max-width: 760px; width: calc(100% - 32px); padding: 10px 14px; border-radius: 10px;
  background: #fdf0ed; border: 1px solid #f2cfc7; color: #8f2a1d; font-size: 14px;
}
.banner.info { background: #fff8e8; border-color: #f1e0b5; color: #7a5a12; }
.banner a { color: inherit; font-weight: 600; }

/* ---------- views ---------- */
.view { flex: 1; min-height: 0; }

.onboarding { display: grid; place-items: center; padding: 32px 16px; }
.onboard-card {
  width: min(520px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  padding: 36px; box-shadow: var(--shadow); text-align: center;
}
.onboard-card img { border-radius: 16px; }
.onboard-card h1 { font-family: var(--serif); font-weight: 500; font-size: 28px; margin: 14px 0 8px; }
.onboard-card p { color: var(--muted); margin: 0 0 22px; text-wrap: pretty; }
#keyForm { display: flex; gap: 8px; }
#keyForm input {
  flex: 1; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-family: var(--mono); font-size: 14px;
}
#keyForm input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.form-error { color: var(--err); margin: 12px 0 0 !important; font-size: 14px; }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; }
.transcript { flex: 1; overflow-y: auto; padding: 28px 16px 12px; scroll-behavior: smooth; }
.transcript > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 12vh; color: var(--muted); }
.empty img { border-radius: 14px; }
.empty h2 { font-family: var(--serif); font-weight: 500; font-size: 30px; color: var(--text); margin: 14px 0 4px; }

.msg { margin-bottom: 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 16px; border-radius: 18px 18px 4px 18px; max-width: 80%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant { display: flex; gap: 12px; align-items: flex-start; }
.msg.assistant .avatar { flex: none; width: 28px; height: 28px; border-radius: 8px; margin-top: 2px; }
.msg.assistant .body { flex: 1; min-width: 0; }
.msg.assistant.streaming .avatar { animation: spin-glow 2.4s ease-in-out infinite; }
@keyframes spin-glow { 50% { transform: rotate(12deg) scale(1.06); filter: saturate(1.3); } }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3 { font-family: var(--serif); font-weight: 600; line-height: 1.3; margin: 18px 0 8px; }
.md h1 { font-size: 22px; } .md h2 { font-size: 19px; } .md h3 { font-size: 17px; }
.md ul, .md ol { padding-left: 22px; margin: 0 0 12px; }
.md li { margin: 3px 0; }
.md a { color: var(--accent-strong); }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.md pre {
  background: #1f1a17; color: #f6ece4; padding: 14px 16px; border-radius: 12px; overflow-x: auto; font-size: 13.5px; line-height: 1.5;
}
.md pre code { background: none; padding: 0; color: inherit; }
.md table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.md th { background: var(--surface-2); }
.md blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }

.cursor::after { content: "▍"; color: var(--accent); animation: blink 1s steps(1) infinite; margin-left: 1px; }
@keyframes blink { 50% { opacity: 0; } }

details.thinking, details.tool {
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); margin: 0 0 12px; overflow: hidden;
}
details.thinking summary, details.tool summary {
  cursor: pointer; list-style: none; padding: 8px 12px; font-size: 13.5px; color: var(--muted);
  display: flex; align-items: center; gap: 8px; user-select: none;
}
details summary::-webkit-details-marker { display: none; }
details summary .chev { margin-left: auto; transition: transform .15s; }
details[open] summary .chev { transform: rotate(90deg); }
.thinking-text { padding: 0 14px 12px; font-size: 13.5px; color: var(--muted); white-space: pre-wrap; }
details.tool summary .tname { font-family: var(--mono); color: var(--text); font-size: 13px; }
details.tool .tool-body { padding: 0 12px 12px; display: grid; gap: 8px; }
details.tool .tool-body h4 { margin: 0; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
details.tool pre {
  margin: 0; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
  font: 12.5px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 320px; overflow: auto;
}
.tool-state { width: 16px; height: 16px; flex: none; display: grid; place-items: center; }
.tool-state.running { border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
.tool-state.ok { color: var(--ok); }
.tool-state.err { color: var(--err); }
@keyframes spin { to { transform: rotate(360deg); } }

.notice { font-size: 13px; color: var(--muted); background: var(--surface-2); border-radius: 8px; padding: 6px 10px; margin: 0 0 12px; }
.error-msg { font-size: 14px; color: var(--err); background: #fdf0ed; border: 1px solid #f2cfc7; border-radius: 10px; padding: 8px 12px; margin: 0 0 12px; }

.composer-wrap { padding: 8px 16px 12px; }
.composer {
  max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  box-shadow: var(--shadow); padding: 12px 12px 8px 16px; transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: #d9cbbd; box-shadow: 0 0 0 4px rgba(226, 100, 59, 0.08), var(--shadow); }
.composer textarea {
  width: 100%; border: 0; outline: 0; resize: none; background: transparent; max-height: 240px; min-height: 26px; padding: 2px 0;
}
.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; }
.composer-left, .composer-right { display: flex; align-items: center; gap: 8px; }
.model-picker select {
  appearance: none; border: 1px solid var(--line); background: var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%237a6e66' stroke-width='1.5' fill='none'/%3E%3C/svg%3E") no-repeat right 10px center;
  padding: 5px 28px 5px 10px; border-radius: 9px; font-size: 13px; cursor: pointer; max-width: 220px;
}
.tools-chip {
  font-size: 12.5px; color: var(--muted); padding: 4px 9px; border-radius: 999px; background: var(--surface-2);
  font-variant-numeric: tabular-nums; cursor: pointer;
}
.tools-chip.on { color: var(--ok); background: #e7f5ee; }
.send {
  width: 34px; height: 34px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; display: grid; place-items: center;
  cursor: pointer; transition: background-color .15s, transform .1s;
}
.send:hover { background: var(--accent-strong); }
.send:active { transform: scale(0.94); }
.send:disabled { background: #e9c7b9; cursor: default; }
.send.stop { background: var(--text); }
.hint { max-width: 760px; margin: 6px auto 0; text-align: center; font-size: 12px; color: var(--muted); }

/* ---------- tools ---------- */
.tools { overflow-y: auto; padding: 28px 20px; }
.tools-head, .tool-grid { max-width: 1080px; margin: 0 auto; }
.tools-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.tools-head h2 { font-family: var(--serif); font-weight: 500; font-size: 26px; margin: 0; }
.tools-head p { margin: 4px 0 0; }
.tools-actions { display: flex; gap: 8px; }
.tools-actions input { padding: 7px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); min-width: 220px; }
.tool-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.tool-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: 0 1px 2px rgba(0,0,0,.03);
  display: flex; flex-direction: column; gap: 8px; min-width: 0;
}
.tool-card h3 { margin: 0; font: 600 14px/1.4 var(--mono); overflow-wrap: anywhere; }
.tool-card .ttitle { font-size: 13px; color: var(--muted); margin-top: -4px; }
.tool-card p { margin: 0; font-size: 14px; color: #4b403a; white-space: pre-wrap; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }
.params { display: flex; flex-wrap: wrap; gap: 6px; }
.param { font: 12px var(--mono); background: var(--surface-2); border-radius: 6px; padding: 2px 7px; }
.param.req { background: var(--accent-soft); color: var(--accent-strong); }
.param .ptype { color: var(--muted); }
.tool-card details summary { font-size: 12.5px; color: var(--muted); cursor: pointer; }
.tool-card details pre {
  font: 12px/1.45 var(--mono); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px; overflow: auto; max-height: 260px; margin: 6px 0 0;
}
.tools-empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 48px 16px; border: 1px dashed var(--line); border-radius: var(--radius); }

/* ---------- dialog ---------- */
dialog { border: 0; border-radius: 18px; padding: 0; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
dialog::backdrop { background: rgba(42, 33, 29, .35); }
.settings-card { padding: 24px; width: min(420px, 90vw); }
.settings-card h2 { margin: 0 0 8px; font-family: var(--serif); font-weight: 500; }
.settings-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* ---------- OAuth callback page ---------- */
body.callback { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.callback-card { background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 32px; text-align: center; max-width: 420px; box-shadow: var(--shadow); }
.callback-card img { border-radius: 14px; }
.callback-card h1 { font-family: var(--serif); font-weight: 500; font-size: 22px; margin: 12px 0 6px; }
.callback-card p { margin: 6px 0; overflow-wrap: anywhere; }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .top-actions { width: 100%; }
  .mcp-status { flex: 1; max-width: none; }
  .brand-tag, .hint { display: none; }
  .msg.user .bubble { max-width: 92%; }
}

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