:root {
  --bg: #faf9f6;
  --surface: #ffffff;
  --surface-2: #f3f1ec;
  --border: #e4e0d8;
  --text: #1f1e1c;
  --muted: #6b6760;
  --accent: #c2562f;
  --accent-soft: #f6e6de;
  --ok: #2f7d4f;
  --err: #b3261e;
  --err-soft: #fbe9e7;
  --code-bg: #f3f1ec;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 12px;
  --font: "Inter", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1918;
    --surface: #232220;
    --surface-2: #2c2a27;
    --border: #3a3734;
    --text: #ece9e3;
    --muted: #a19c93;
    --accent: #e07a52;
    --accent-soft: #3d2a21;
    --ok: #6cc08b;
    --err: #f2867d;
    --err-soft: #3c2220;
    --code-bg: #2c2a27;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1918; --surface: #232220; --surface-2: #2c2a27; --border: #3a3734;
  --text: #ece9e3; --muted: #a19c93; --accent: #e07a52; --accent-soft: #3d2a21;
  --ok: #6cc08b; --err: #f2867d; --err-soft: #3c2220; --code-bg: #2c2a27;
  --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
}

* { 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;
  overflow: hidden;
}
button { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: var(--surface); gap: 12px; flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo svg { width: 28px; height: 28px; display: block; }
.logo rect { fill: var(--accent); }
.logo path { stroke: #fff; stroke-width: 2.6; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.brand-name { font-weight: 600; font-size: 16px; white-space: nowrap; }
.model-chip {
  font: 500 12px var(--mono); color: var(--muted); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.model-chip:empty { display: none; }
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; }

.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 6px 14px; cursor: pointer; font-weight: 500; font-size: 14px;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .starter:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 3px 9px; font-size: 13px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--err); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Layout */
.layout { flex: 1; display: flex; min-height: 0; }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.messages {
  flex: 1; overflow-y: auto; padding: 24px 16px 8px;
  display: flex; flex-direction: column; gap: 20px;
}
.messages > * { width: 100%; max-width: 780px; margin: 0 auto; }

/* Empty state */
.empty { margin-top: 8vh; text-align: center; }
.empty h1 { font-size: 28px; font-weight: 600; margin: 0 0 8px; letter-spacing: -0.01em; }
.empty p { color: var(--muted); max-width: 540px; margin: 0 auto 28px; }
.starters { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }
.starter {
  display: flex; flex-direction: column; align-items: flex-start; text-align: left; gap: 2px; padding: 14px 16px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); cursor: pointer; box-shadow: var(--shadow);
  transition: border-color .15s, transform .15s;
}
.starter:hover { border-color: var(--accent); transform: translateY(-1px); }
.starter strong { font-weight: 600; font-size: 14px; }
.starter span { color: var(--muted); font-size: 13px; }

/* Messages */
.msg-user {
  align-self: flex-end; background: var(--accent-soft); border-radius: var(--radius);
  padding: 10px 14px; white-space: pre-wrap; word-wrap: break-word;
  max-width: min(780px, 100%);
}
.msg-user-wrap { display: flex; justify-content: flex-end; }
.msg-assistant { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { margin: 20px 0 8px; line-height: 1.3; font-weight: 600; }
.md h1 { font-size: 20px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md code { font: 13px var(--mono); background: var(--code-bg); border-radius: 4px; padding: 1px 5px; }
.md blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.md table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.md a { color: var(--accent); }

.codeblock { position: relative; margin: 0 0 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--code-bg); }
.codeblock pre { margin: 0; padding: 14px; overflow-x: auto; }
.codeblock pre code { background: none; padding: 0; font-size: 13px; line-height: 1.55; white-space: pre-wrap; word-break: break-word; }
.code-actions {
  display: flex; justify-content: flex-end; gap: 4px; padding: 4px 6px;
  border-bottom: 1px solid var(--border); font-size: 12px;
}
.code-actions .lang { margin-right: auto; padding: 3px 6px; color: var(--muted); font: 12px var(--mono); }

/* Reasoning */
.thinking { border-left: 2px solid var(--border); padding-left: 12px; color: var(--muted); font-size: 14px; }
.thinking summary { cursor: pointer; user-select: none; font-weight: 500; list-style: none; display: flex; align-items: center; gap: 6px; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary::before { content: "▸"; font-size: 11px; transition: transform .15s; }
.thinking[open] summary::before { transform: rotate(90deg); }
.thinking .thinking-body { white-space: pre-wrap; margin-top: 6px; }

/* Tool cards */
.tool {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow); overflow: hidden;
}
.tool-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; font-size: 14px; }
.tool-head .title { font-weight: 600; }
.tool-head .sub { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.tool-status { width: 16px; height: 16px; flex-shrink: 0; display: grid; place-items: center; }
.tool.running .tool-status::before {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
.tool.ok .tool-status::before { content: "✓"; color: var(--ok); font-weight: 700; }
.tool.error .tool-status::before { content: "!"; color: var(--err); font-weight: 700; }
@keyframes spin { to { transform: rotate(360deg); } }
.tool-body { border-top: 1px solid var(--border); padding: 10px 14px; font-size: 14px; }
.tool-body:empty { display: none; }
.tool-error { color: var(--err); white-space: pre-wrap; }

.notice { font-size: 13px; color: var(--muted); padding: 8px 12px; border-radius: 8px; background: var(--surface-2); }
.notice.error { color: var(--err); background: var(--err-soft); }
.usage { font: 12px var(--mono); color: var(--muted); }

.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* Composer */
.composer {
  display: flex; align-items: flex-end; gap: 8px; width: calc(100% - 32px); max-width: 780px;
  margin: 8px auto 0; padding: 8px 8px 8px 14px; border: 1px solid var(--border);
  border-radius: 16px; background: var(--surface); box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; resize: none; background: transparent; color: var(--text);
  font: 15px/1.5 var(--font); max-height: 240px; padding: 6px 0; outline: none;
}
.send { width: 38px; height: 38px; padding: 0; display: grid; place-items: center; border-radius: 10px; flex-shrink: 0; }
.send svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 2.2; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.hint { text-align: center; font-size: 12px; color: var(--muted); margin: 6px 16px 12px; }

/* Library */
.library {
  width: 300px; flex-shrink: 0; border-left: 1px solid var(--border);
  background: var(--surface); display: flex; flex-direction: column; min-height: 0;
}
.library-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; }
.library h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0; }
.close-library { display: none; }
.library-list { list-style: none; margin: 0; padding: 0 8px 16px; overflow-y: auto; }
.library-list li button {
  width: 100%; text-align: left; border: 0; background: none; border-radius: 8px;
  padding: 8px 10px; cursor: pointer; display: flex; flex-direction: column; gap: 1px;
}
.library-list li button:hover { background: var(--surface-2); }
.library-list .name { font-weight: 500; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-list .meta { font-size: 12px; color: var(--muted); }
.library-empty { color: var(--muted); font-size: 13px; padding: 0 16px; }
.library-empty[hidden] { display: none; }

/* Dialog */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(720px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; max-height: 85vh; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog-head h3 { margin: 0; font-size: 17px; overflow-wrap: anywhere; }
.dialog-head select { font: inherit; font-size: 13px; padding: 3px 6px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.dialog-head select[hidden] { display: none; }
#dialog-content { overflow-y: auto; display: grid; gap: 12px; }
#dialog-content .label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 4px; }
#dialog-content pre { margin: 0; white-space: pre-wrap; word-break: break-word; font: 13px/1.55 var(--mono); background: var(--code-bg); padding: 10px 12px; border-radius: 8px; }
#dialog-content input, #dialog-content textarea {
  width: 100%; font: inherit; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
#dialog-content textarea { font: 13px/1.5 var(--mono); min-height: 160px; resize: vertical; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dialog-actions .spacer { flex: 1; }

/* Narrow screens: library becomes an overlay drawer */
@media (max-width: 900px) {
  .library {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(320px, 88vw); z-index: 20;
    transform: translateX(100%); transition: transform .2s; box-shadow: -8px 0 30px rgba(0,0,0,.15);
  }
  .library.open { transform: none; }
  .close-library { display: inline-block; }
}
@media (min-width: 901px) {
  #library-toggle { display: none; }
}
@media (max-width: 560px) {
  .starters { grid-template-columns: 1fr; }
  .empty { margin-top: 3vh; }
  .empty h1 { font-size: 23px; }
  .model-chip { display: none; }
  .messages { padding: 16px 16px 8px; }
}
