:root {
  --bg: #faf9f7;
  --panel: #f1efeb;
  --surface: #ffffff;
  --text: #1f1e1d;
  --muted: #6b6862;
  --border: #e2dfd9;
  --accent: #c96442;
  --accent-soft: #f6e3da;
  --accent-text: #ffffff;
  --user-bg: #ece8e1;
  --code-bg: #f4f2ee;
  --danger: #b3261e;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1e1c;
    --panel: #181816;
    --surface: #2a2926;
    --text: #ecebe8;
    --muted: #a09d96;
    --border: #3a3935;
    --accent: #d97757;
    --accent-soft: #4a2f25;
    --user-bg: #34322e;
    --code-bg: #252421;
    --danger: #f2877e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.6; }
.muted { color: var(--muted); }
.error { color: var(--danger); }

.primary {
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: 10px;
  padding: 0.5rem 1.1rem;
  font-weight: 600;
}
.primary:hover:not(:disabled) { filter: brightness(1.05); }
.danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: 10px;
  padding: 0.5rem 1.1rem;
}
.link { background: none; border: 0; color: var(--muted); padding: 0.25rem 0; }
.link:hover { color: var(--text); }
.icon { background: none; border: 0; font-size: 1.25rem; padding: 0.25rem 0.5rem; }

/* ---- login ---- */
.login { min-height: 100%; display: grid; place-items: center; padding: 1rem; }
.login-card {
  width: min(360px, 100%);
  display: grid;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
}
.login-card h1 { margin: 0; font-size: 1.5rem; }
.login-card p { margin: 0; }
.login-card input {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
}

/* ---- layout ---- */
.app { display: grid; grid-template-columns: 260px 1fr; height: 100dvh; }
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0.75rem;
  gap: 0.5rem;
}
.new-chat {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 0.55rem;
  text-align: left;
  font-weight: 600;
}
.convo-list { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 2px; }
.convo { display: flex; align-items: center; border-radius: 8px; }
.convo:hover, .convo.active { background: var(--user-bg); }
.convo-title {
  flex: 1;
  min-width: 0;
  background: none;
  border: 0;
  text-align: left;
  padding: 0.45rem 0.6rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.convo-delete { background: none; border: 0; color: var(--muted); padding: 0 0.6rem; font-size: 1.1rem; opacity: 0; }
.convo:hover .convo-delete, .convo.active .convo-delete { opacity: 1; }
.sidebar-foot { display: flex; justify-content: space-between; border-top: 1px solid var(--border); padding-top: 0.5rem; }
.scrim { display: none; }

.main { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
}
.topbar select {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 0.35rem 0.5rem;
  max-width: 60vw;
}
#toggle-sidebar { display: none; }
.status { font-size: 0.85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- messages ---- */
.messages { flex: 1; overflow-y: auto; padding: 1.5rem 1rem 1rem; }
.messages > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; margin-top: 18vh; }
.empty h2 { font-size: 1.75rem; margin: 0 0 0.25rem; font-weight: 600; }

.msg { margin-bottom: 1.25rem; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; gap: 0.4rem; }
.msg.user .body {
  background: var(--user-bg);
  border-radius: var(--radius);
  padding: 0.6rem 0.9rem;
  white-space: pre-wrap;
  max-width: 85%;
}
.msg.user .body:empty { display: none; }
.msg .error { margin-top: 0.4rem; font-size: 0.9rem; }
.msg-attachments { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 0.4rem; max-width: 85%; }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md pre {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem 1rem;
  overflow-x: auto;
  font-size: 0.875rem;
  line-height: 1.5;
}
.md code { font-family: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace; font-size: 0.9em; }
.md :not(pre) > code { background: var(--code-bg); padding: 0.1em 0.35em; border-radius: 5px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 0.35rem 0.6rem; }
.md blockquote { margin: 0; padding-left: 1rem; border-left: 3px solid var(--border); color: var(--muted); }
.md a { color: var(--accent); }
.md img { max-width: 100%; }

.thinking {
  border-left: 2px solid var(--border);
  padding-left: 0.8rem;
  margin-bottom: 0.6rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.thinking summary { cursor: pointer; user-select: none; }

.activity { list-style: none; margin: 0 0 0.6rem; padding: 0; font-size: 0.85rem; color: var(--muted); display: grid; gap: 0.15rem; }

.sources {
  margin-top: 0.75rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.85rem;
}
.sources-title { font-weight: 600; margin-bottom: 0.25rem; }
.sources ol { margin: 0; padding-left: 1.3rem; display: grid; gap: 0.15rem; }
.sources a { color: var(--accent); text-decoration: none; }
.sources a:hover { text-decoration: underline; }

.meta {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.4rem;
  font-size: 0.8rem;
  color: var(--muted);
}
.copy {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 0.1rem 0.45rem;
}
pre .copy { position: absolute; top: 0.4rem; right: 0.4rem; background: var(--surface); opacity: 0; }
pre:hover .copy { opacity: 1; }

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

/* ---- attachment chips ---- */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  max-width: 240px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.25rem 0.4rem;
  font-size: 0.8rem;
}
.chip-thumb { width: 36px; height: 36px; object-fit: cover; border-radius: 6px; }
.chip-icon {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.65rem;
  font-weight: 700;
}
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-remove { background: none; border: 0; color: var(--muted); padding: 0 0.2rem; font-size: 1rem; }
.chip.busy { opacity: 0.6; }
.chip.bad { border-color: var(--danger); color: var(--danger); }

/* ---- composer ---- */
.composer {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  width: min(760px, calc(100% - 2rem));
  margin: 0 auto 1rem;
  padding: 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
}
.attachments { display: flex; flex-wrap: wrap; gap: 0.4rem; padding: 0.15rem 0.15rem 0; }
.composer textarea {
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  padding: 0.4rem 0.5rem;
  max-height: 240px;
}
.composer-bar { display: flex; align-items: center; gap: 0.4rem; }
.spacer { flex: 1; }
.tool {
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.25rem 0.7rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.tool:hover { color: var(--text); }
.toggle[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.drop-overlay {
  position: absolute;
  inset: 0.5rem;
  z-index: 5;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
}

/* ---- settings ---- */
dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  width: min(520px, calc(100% - 2rem));
  padding: 0;
}
dialog::backdrop { background: rgb(0 0 0 / 0.35); }
.settings { display: grid; gap: 1rem; padding: 1.5rem; }
.settings h2 { margin: 0; font-size: 1.2rem; }
.settings label { display: grid; gap: 0.35rem; font-weight: 600; font-size: 0.9rem; }
.settings label.check { display: flex; align-items: center; gap: 0.5rem; font-weight: 400; }
.settings textarea, .settings select {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 0.5rem 0.7rem;
  font-weight: 400;
}
.settings menu { margin: 0; padding: 0; display: flex; justify-content: flex-end; }

/* ---- compare mode ---- */
.model-chips { display: flex; flex-wrap: wrap; gap: 0.35rem; min-width: 0; }
.model-chip { white-space: nowrap; }
.messages > .msg.multi { max-width: 1400px; }
.compare-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 0.75rem;
  align-items: start;
}
.compare-grid .reply {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 0.9rem;
}
.compare-grid .reply.best { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.reply-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: -0.15rem 0 0.6rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}
.reply-model { font-weight: 700; }
.reply-stats { color: var(--muted); flex: 1; }
.vote {
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.1rem 0.55rem;
  font-size: 0.78rem;
  color: var(--muted);
}
.vote:hover { color: var(--text); }
.vote.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.scoreboard { border-top: 1px solid var(--border); padding-top: 0.5rem; font-size: 0.8rem; display: grid; gap: 0.2rem; }
.scoreboard-title { font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-size: 0.7rem; }
.score-row { display: grid; grid-template-columns: 1fr auto; column-gap: 0.5rem; }
.score-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-wins { color: var(--accent); font-weight: 700; }
.score-meta { grid-column: 1 / -1; color: var(--muted); }
.settings .spacer { flex: 1; }

/* ---- files created by models ---- */
.files { display: grid; gap: 0.4rem; margin-top: 0.75rem; }
.file-card {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.file-name { flex: 1; min-width: 0; font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.primary.small { padding: 0.3rem 0.75rem; font-size: 0.82rem; }

/* ---- mobile ---- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  #toggle-sidebar { display: inline-block; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(280px, 85vw);
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar-open .sidebar { transform: none; }
  .sidebar-open .scrim { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / 0.35); z-index: 10; }
  .convo-delete { opacity: 1; }
  .tool span { display: none; }
}
