:root {
  --bg: #ffffff;
  --bg-sunken: #f6f7f9;
  --bg-raised: #ffffff;
  --border: #e3e6ea;
  --text: #16181d;
  --text-dim: #666e7a;
  --accent: #5b5bd6;
  --accent-soft: #ececfb;
  --user-bubble: #5b5bd6;
  --user-text: #ffffff;
  --think-bg: #f4f2fb;
  --think-border: #ded8f2;
  --danger: #c0392b;
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --bg-sunken: #1b1e23;
    --bg-raised: #212429;
    --border: #2e333b;
    --text: #e8eaed;
    --text-dim: #9aa2ad;
    --accent: #8b8bf0;
    --accent-soft: #24243a;
    --user-bubble: #4f4fc4;
    --user-text: #ffffff;
    --think-bg: #1e1c2b;
    --think-border: #332f47;
    --danger: #ff7a6b;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

a { color: var(--accent); }

/* ---------- sidebar ---------- */

#sidebar {
  width: 290px;
  flex: none;
  background: var(--bg-sunken);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 18px 16px;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand .logo { font-size: 26px; }

.brand h1 {
  font-size: 16px;
  margin: 0;
  letter-spacing: -0.01em;
}

.brand .sub {
  margin: 1px 0 0;
  font-size: 12px;
  color: var(--text-dim);
}

.panel h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-dim);
  margin: 0 0 10px;
  font-weight: 600;
}

.panel { border-top: 1px solid var(--border); padding-top: 16px; }

.field {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
}

.field > span {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
  color: var(--text-dim);
}

.field output { font-variant-numeric: tabular-nums; color: var(--text); }

.toggle-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.toggle-field > span { margin: 0; color: var(--text); font-weight: 500; }

input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }

select, textarea, input[type="password"] {
  width: 100%;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
}

textarea { resize: vertical; }

select:focus, textarea:focus, input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.hint {
  font-size: 12px;
  color: var(--text-dim);
  margin: 6px 0 0;
}

.btn {
  width: 100%;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text);
  cursor: pointer;
}

.btn:hover { border-color: var(--accent); }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.primary:hover { filter: brightness(1.08); }

.btn.subtle { background: transparent; color: var(--text-dim); }

.token-fallback { margin-top: 10px; }

.token-fallback summary {
  font-size: 12px;
  color: var(--text-dim);
  cursor: pointer;
}

.token-fallback input { margin: 8px 0 6px; }

.user-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  font-size: 13px;
  font-weight: 500;
}

.user-row img {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--border);
}

.sidebar-foot { margin-top: auto; border-top: 1px solid var(--border); padding-top: 14px; }

/* ---------- main ---------- */

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

#chat {
  flex: 1;
  overflow-y: auto;
  padding: 28px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

#welcome {
  margin: auto;
  text-align: center;
  max-width: 560px;
  padding-bottom: 40px;
}

#welcome h2 {
  font-size: 21px;
  font-weight: 600;
  letter-spacing: -0.015em;
  margin: 0 0 6px;
}

#examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 22px;
}

.example {
  font: inherit;
  font-size: 13px;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text);
  cursor: pointer;
}

.example:hover { border-color: var(--accent); color: var(--accent); }

.msg {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg.user { align-items: flex-end; }

.bubble {
  padding: 11px 15px;
  border-radius: var(--radius);
  max-width: 88%;
  overflow-wrap: anywhere;
}

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.msg.assistant .bubble {
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  max-width: 100%;
}

.msg.error .bubble {
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
}

.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.msg-images img {
  max-width: 190px;
  max-height: 190px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

/* thinking block */

.think {
  border: 1px solid var(--think-border);
  background: var(--think-bg);
  border-radius: var(--radius);
  max-width: 100%;
  font-size: 13.5px;
}

.think > summary {
  cursor: pointer;
  padding: 8px 13px;
  color: var(--text-dim);
  font-size: 12.5px;
  font-weight: 500;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 7px;
}

.think > summary::-webkit-details-marker { display: none; }

.think > summary::before {
  content: "▸";
  transition: transform 0.15s;
}

.think[open] > summary::before { transform: rotate(90deg); }

.think .think-body {
  padding: 0 13px 11px;
  color: var(--text-dim);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 340px;
  overflow-y: auto;
}

/* markdown */

.bubble > *:first-child { margin-top: 0; }
.bubble > *:last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 10px; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin-bottom: 3px; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 {
  margin: 16px 0 8px;
  line-height: 1.3;
  font-size: 1.05em;
}
.bubble code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--accent-soft);
  padding: 1.5px 5px;
  border-radius: 5px;
}
.bubble pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 11px 13px;
  overflow-x: auto;
  margin: 0 0 10px;
}
.bubble pre code { background: none; padding: 0; font-size: 0.85em; }
.bubble table { border-collapse: collapse; margin-bottom: 10px; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.bubble blockquote {
  margin: 0 0 10px;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--text-dim);
}

.cursor::after {
  content: "▋";
  animation: blink 1s steps(2) infinite;
  margin-left: 1px;
  color: var(--accent);
}

@keyframes blink { 50% { opacity: 0; } }

.usage {
  font-size: 11.5px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* ---------- composer ---------- */

#composer-wrap {
  padding: 10px 20px 16px;
  background: var(--bg);
}

#attachments {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#attachments:not(:empty) { margin-bottom: 9px; }

.thumb { position: relative; }

.thumb img {
  width: 62px;
  height: 62px;
  object-fit: cover;
  border-radius: 9px;
  border: 1px solid var(--border);
  display: block;
}

.thumb button {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text);
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  padding: 0;
}

#composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px;
}

#composer:focus-within { border-color: var(--accent); }

#input {
  flex: 1;
  border: none;
  background: none;
  resize: none;
  padding: 8px 6px;
  font: inherit;
  color: var(--text);
  max-height: 190px;
  overflow-y: auto;
}

#input:focus { outline: none; }

.icon-btn {
  flex: none;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn:hover { background: var(--border); color: var(--text); }

.icon-btn.send { background: var(--accent); color: #fff; }
.icon-btn.send:hover { filter: brightness(1.08); }
.icon-btn.send:disabled { opacity: 0.4; cursor: default; filter: none; }
.icon-btn.stop { background: var(--danger); color: #fff; }

#status {
  max-width: 760px;
  margin: 7px auto 0;
  text-align: center;
  min-height: 16px;
}

#status.error { color: var(--danger); }

/* drag + drop */

#drop-veil {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
  border: 2px dashed var(--accent);
  border-radius: 12px;
  margin: 8px;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
  z-index: 5;
}

body.dragging #drop-veil { display: grid; }

/* ---------- responsive ---------- */

@media (max-width: 820px) {
  body { flex-direction: column; overflow: auto; }
  #sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 46vh;
  }
  #main { min-height: 54vh; }
  .bubble { max-width: 94%; }
}
