:root {
  --serif: "Fraunces", "Iowan Old Style", Georgia, serif;
  --mono: "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;

  --maxw: 880px;

  /* Per-theme palette (defaults = Terracotta dark; overridden below). */
  --bg: #0e0e10;
  --bg-elev: #16161a;
  --bg-input: #1c1c21;
  --fg: #e8e6e1;
  --fg-dim: #8a8780;
  --fg-faint: #54524d;
  --rule: #2a2a30;
  --rule-strong: #3a3a42;
  --accent: #d97757;        /* terracotta — warm, distinctive */
  --accent-dim: #8a4d38;
  --accent-fg: #0e0e10;     /* foreground colour to use ON the accent */
  --bot: #c5a572;           /* muted gold for bot messages */
  --me: #7ba7bc;            /* cool blue for self */
  --danger: #c4584a;
  --ok: #7ba87b;
  --bg-glow:
    radial-gradient(circle at 20% 0%, rgba(217, 119, 87, 0.04) 0%, transparent 50%),
    radial-gradient(circle at 80% 100%, rgba(197, 165, 114, 0.03) 0%, transparent 50%);
}

/* Terracotta dark — original look. Repeated so an explicit pick of this
   theme overrides any inherited values. */
body[data-theme="terracotta"] {
  --bg: #0e0e10;
  --bg-elev: #16161a;
  --bg-input: #1c1c21;
  --fg: #e8e6e1;
  --fg-dim: #8a8780;
  --fg-faint: #54524d;
  --rule: #2a2a30;
  --rule-strong: #3a3a42;
  --accent: #d97757;
  --accent-dim: #8a4d38;
  --accent-fg: #0e0e10;
  --bot: #c5a572;
  --me: #7ba7bc;
  --danger: #c4584a;
  --ok: #7ba87b;
  --bg-glow:
    radial-gradient(circle at 20% 0%, rgba(217, 119, 87, 0.04) 0%, transparent 50%),
    radial-gradient(circle at 80% 100%, rgba(197, 165, 114, 0.03) 0%, transparent 50%);
}

/* Slack-inspired light theme — aubergine accent, paper background. */
body[data-theme="slack"] {
  --bg: #f7f7f8;
  --bg-elev: #ffffff;
  --bg-input: #ffffff;
  --fg: #1d1c1d;
  --fg-dim: #616061;
  --fg-faint: #868686;
  --rule: #e3e3e6;
  --rule-strong: #c8c8cc;
  --accent: #611f69;
  --accent-dim: #4a154b;
  --accent-fg: #ffffff;
  --bot: #c98a00;
  --me: #007a5a;
  --danger: #e01e5a;
  --ok: #2eb67d;
  --bg-glow: none;
}

/* Discord-inspired dark theme — blurple accent. */
body[data-theme="discord"] {
  --bg: #313338;
  --bg-elev: #2b2d31;
  --bg-input: #383a40;
  --fg: #f2f3f5;
  --fg-dim: #b5bac1;
  --fg-faint: #80848e;
  --rule: #3f4248;
  --rule-strong: #4e5058;
  --accent: #5865f2;
  --accent-dim: #4752c4;
  --accent-fg: #ffffff;
  --bot: #f0b232;
  --me: #57f287;
  --danger: #ed4245;
  --ok: #57f287;
  --bg-glow: none;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  min-height: 100vh;
  background-image: var(--bg-glow);
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

/* ---------- Header ---------- */
header.topbar {
  border-bottom: 1px solid var(--rule);
  padding: 14px 24px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 24px;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

/* Topbar brand: clickable link wrapping the mark + a two-line text
   block (wordmark on top, tagline underneath). */
a.brand,
a.brand:visited {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: inherit;
}
.brand .brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}
.brand .brand-name {
  font-family: var(--serif);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.005em;
  color: var(--fg);
  transition: color 100ms;
}
.brand .brand-tagline {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--fg-faint);
  letter-spacing: 0.04em;
  margin-top: 2px;
}
a.brand:hover .brand-name { color: var(--accent); }
a.brand:hover .brand-tagline { color: var(--fg-dim); }

/* Shared brand mark — flex wrapper around the inline SVG. */
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  flex-shrink: 0;
}
.brand-mark svg { width: 100%; height: 100%; display: block; }

/* Auth/card wordmark heading variant. */
.card h1.wordmark {
  display: flex;
  align-items: center;
  gap: 12px;
}
.card h1.wordmark .brand-mark { color: var(--accent); }
.card .wordmark-tagline {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--fg-faint);
  margin: 4px 0 18px;
}

.repo-crumbs {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--fg-dim);
  display: flex;
  align-items: center;
}
.repo-crumbs a { color: var(--fg); text-decoration: none; border-bottom: 1px dotted var(--fg-faint); }
.repo-crumbs a:hover { color: var(--accent); border-color: var(--accent); }
.repo-crumbs .sep { color: var(--fg-faint); margin: 0 6px; }
.repo-crumbs .switch-repo {
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 4px 10px;
  margin-left: 14px;
  cursor: pointer;
  transition: background 100ms, color 100ms;
}
.repo-crumbs .switch-repo:hover { background: var(--accent); color: var(--accent-fg); }

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--fg-dim);
}
.user-chip img { width: 20px; height: 20px; border: 1px solid var(--rule-strong); }
.user-chip button {
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--fg-dim);
  font-family: var(--mono);
  font-size: 11px;
  padding: 4px 8px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.user-chip button:hover { color: var(--fg); border-color: var(--rule-strong); }

/* ---------- Layout ---------- */
main.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  flex: 1;
  min-height: 0;
}
main.layout.with-participants {
  grid-template-columns: 280px 1fr 220px;
}
@media (max-width: 1080px) {
  main.layout.with-participants { grid-template-columns: 280px 1fr; }
  aside.participants { display: none; }
}
@media (max-width: 760px) {
  main.layout,
  main.layout.with-participants { grid-template-columns: 1fr; }
  aside.thread-list { display: none; }
  aside.thread-list.open { display: block; position: fixed; inset: 56px 0 0 0; z-index: 5; background: var(--bg); }
}

aside.thread-list {
  border-right: 1px solid var(--rule);
  overflow-y: auto;
  background: var(--bg);
}
.thread-list-header {
  padding: 14px 18px 8px;
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--fg-faint);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.thread-list-header button {
  background: transparent;
  border: none;
  color: var(--accent);
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  cursor: pointer;
}
.thread-list-header button:hover { color: var(--fg); }

.thread-item {
  padding: 12px 18px;
  border-top: 1px solid var(--rule);
  cursor: pointer;
  transition: background 100ms;
}
.thread-item:hover { background: var(--bg-elev); }
.thread-item.active { background: var(--bg-elev); border-left: 2px solid var(--accent); padding-left: 16px; }
.thread-item .ti-num {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--fg-faint);
}
.thread-item .ti-title {
  font-family: var(--serif);
  font-size: 14px;
  font-weight: 500;
  margin-top: 2px;
  color: var(--fg);
  line-height: 1.3;
}
.thread-item .ti-meta {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--fg-dim);
  margin-top: 4px;
  display: flex;
  gap: 10px;
}

/* ---------- Chat panel ---------- */
section.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.chat-header {
  border-bottom: 1px solid var(--rule);
  padding: 18px 32px;
  background: var(--bg);
}
.chat-header .title {
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.2;
}
.chat-header .sub {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--fg-dim);
  margin-top: 6px;
  display: flex;
  gap: 16px;
  align-items: center;
}
.chat-header .sub a { color: var(--accent); text-decoration: none; }
.chat-header .sub a:hover { text-decoration: underline; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 32px 12px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.msg {
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 14px;
  max-width: var(--maxw);
}
.msg .avatar {
  width: 28px;
  height: 28px;
  border: 1px solid var(--rule-strong);
  background: var(--bg-elev);
  overflow: hidden;
}
.msg .avatar img { width: 100%; height: 100%; display: block; }

.msg .head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 4px;
}
.msg .author {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
}
.msg.bot .author { color: var(--bot); }
.msg.me .author { color: var(--me); }
.msg .stamp {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--fg-faint);
}
.msg .badge {
  font-family: var(--mono);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  padding: 1px 6px;
  border: 1px solid var(--bot);
  color: var(--bot);
}

.msg .body {
  font-family: var(--serif);
  font-size: 15px;
  line-height: 1.6;
  color: var(--fg);
  word-wrap: break-word;
}
.msg .body p { margin-bottom: 0.5em; }
.msg .body p:last-child { margin-bottom: 0; }
.msg .body code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--bg-elev);
  padding: 1px 5px;
  border: 1px solid var(--rule);
}
.msg .body pre {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--bg-elev);
  border: 1px solid var(--rule);
  padding: 12px 14px;
  overflow-x: auto;
  margin: 8px 0;
  line-height: 1.5;
}
.msg .body pre code { background: none; border: none; padding: 0; }
.msg .body a { color: var(--accent); }
.msg .body blockquote {
  border-left: 2px solid var(--rule-strong);
  padding-left: 12px;
  color: var(--fg-dim);
  margin: 6px 0;
  font-style: italic;
}
.msg .body ul, .msg .body ol { margin: 6px 0 6px 22px; }
.msg .body h1, .msg .body h2, .msg .body h3 {
  font-family: var(--serif);
  margin: 8px 0 4px;
  font-weight: 600;
}
.msg .body h1 { font-size: 1.3em; }
.msg .body h2 { font-size: 1.15em; }
.msg .body h3 { font-size: 1em; }

/* Nested reply indent */
.msg.reply { margin-left: 28px; padding-left: 14px; border-left: 1px solid var(--rule); }

/* "reply" affordance shown inline in the head of top-level comments */
.msg .head .reply-link {
  background: transparent;
  border: none;
  color: var(--fg-faint);
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  cursor: pointer;
  padding: 1px 4px;
  margin-left: auto;
}
.msg .head { align-items: center; }
.msg .head .reply-link:hover { color: var(--accent); }

/* Inline reply composer that appears below a comment */
.reply-composer {
  margin-left: 42px;
  margin-top: -10px;
  margin-bottom: 4px;
  padding: 10px 12px;
  border-left: 2px solid var(--accent);
  background: var(--bg-elev);
  max-width: var(--maxw);
}
.reply-composer textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--rule);
  color: var(--fg);
  font-family: var(--serif);
  font-size: 14px;
  line-height: 1.5;
  padding: 10px 12px;
  resize: vertical;
  min-height: 56px;
  max-height: 200px;
  outline: none;
}
.reply-composer textarea:focus { border-color: var(--rule-strong); }
.reply-composer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 6px;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--fg-faint);
}
.reply-composer-row .hint kbd {
  font-family: var(--mono);
  background: var(--bg);
  border: 1px solid var(--rule);
  padding: 1px 4px;
  font-size: 9px;
  color: var(--fg-dim);
}
.reply-composer-actions { display: flex; gap: 6px; }
.reply-composer-actions button {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 5px 10px;
  cursor: pointer;
  border: 1px solid transparent;
}
.reply-composer-actions button.send {
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
}
.reply-composer-actions button.send:hover { background: var(--accent-dim); }
.reply-composer-actions button.ghost {
  background: transparent;
  border-color: var(--rule);
  color: var(--fg-dim);
}
.reply-composer-actions button.ghost:hover { color: var(--fg); border-color: var(--rule-strong); }

/* Bot pending bubble */
.msg.pending .body { color: var(--fg-dim); font-style: italic; }
.msg.pending .body::after {
  content: '▊';
  animation: blink 1s step-end infinite;
  margin-left: 2px;
  color: var(--bot);
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- Composer ---------- */
.composer {
  border-top: 1px solid var(--rule);
  padding: 16px 32px 20px;
  background: var(--bg);
}
.composer-inner {
  max-width: var(--maxw);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--rule);
  color: var(--fg);
  font-family: var(--serif);
  font-size: 15px;
  line-height: 1.55;
  padding: 12px 14px;
  resize: vertical;
  min-height: 64px;
  max-height: 240px;
  outline: none;
  transition: border-color 120ms;
}
.composer textarea:focus { border-color: var(--rule-strong); }
.composer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--fg-faint);
}
.composer-row .hint kbd {
  font-family: var(--mono);
  background: var(--bg-elev);
  border: 1px solid var(--rule);
  padding: 1px 5px;
  font-size: 10px;
  color: var(--fg-dim);
}
.composer-actions { display: flex; gap: 8px; align-items: center; }
.composer select {
  background: var(--bg-input);
  border: 1px solid var(--rule);
  color: var(--fg-dim);
  font-family: var(--mono);
  font-size: 11px;
  padding: 4px 6px;
  outline: none;
}
.composer button.send {
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 6px 14px;
  cursor: pointer;
  transition: background 100ms;
}
.composer button.send:hover { background: var(--accent-dim); }
.composer button.send:disabled { background: var(--accent-dim); cursor: not-allowed; opacity: 0.6; }

/* Demoted "ask bot" affordance — small icon-style button next to the
   model picker. The /bot slash-command in the composer is the primary
   way to summon now; this is the visual hint that lives in the corner. */
.composer button.bot {
  background: transparent;
  color: var(--bot);
  border: 1px solid var(--bot);
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 600;
  text-transform: lowercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  cursor: pointer;
  opacity: 0.85;
  transition: opacity 100ms, background 100ms, color 100ms;
}
.composer button.bot:hover { background: var(--bot); color: var(--accent-fg); opacity: 1; }
.composer button.bot:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Auth/setup screens ---------- */
.center-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 24px;
}
.card {
  background: var(--bg-elev);
  border: 1px solid var(--rule);
  padding: 32px 36px;
  max-width: 480px;
  width: 100%;
}
.card h1 {
  font-family: var(--serif);
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.015em;
  margin-bottom: 8px;
}
.card .lede {
  font-family: var(--serif);
  font-size: 15px;
  color: var(--fg-dim);
  margin-bottom: 24px;
  line-height: 1.5;
}
.card label {
  display: block;
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--fg-faint);
  margin: 16px 0 6px;
}
.card input[type="text"] {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--rule);
  color: var(--fg);
  font-family: var(--mono);
  font-size: 13px;
  padding: 10px 12px;
  outline: none;
}
.card input[type="text"]:focus { border-color: var(--rule-strong); }
.card button.primary {
  margin-top: 24px;
  width: 100%;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  padding: 12px;
  cursor: pointer;
}
.card button.primary:hover { background: var(--accent-dim); }
.card button.ghost {
  background: transparent;
  border: 1px solid var(--rule-strong);
  color: var(--fg-dim);
  font-family: var(--mono);
  font-size: 11px;
  padding: 8px 12px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-top: 12px;
}
.card button.ghost:hover { color: var(--fg); }

.code-display {
  font-family: var(--mono);
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 0.15em;
  background: var(--bg);
  border: 1px dashed var(--rule-strong);
  padding: 18px;
  text-align: center;
  margin: 16px 0;
  color: var(--accent);
  user-select: all;
}

.banner {
  margin: 12px 0;
  padding: 10px 14px;
  border-left: 2px solid var(--accent);
  background: var(--bg);
  font-family: var(--mono);
  font-size: 12px;
  color: var(--fg-dim);
}
.banner.error { border-color: var(--danger); color: var(--danger); }
.banner.ok { border-color: var(--ok); color: var(--ok); }

/* ---------- Empty / loading ---------- */
.empty {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--fg-dim);
  font-family: var(--mono);
  font-size: 12px;
  padding: 60px 20px;
  text-align: center;
}
.empty .big {
  font-family: var(--serif);
  font-size: 22px;
  color: var(--fg);
  font-weight: 500;
  margin-bottom: 8px;
}

.spinner {
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 1px solid var(--fg-faint);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Scrollbars (webkit) ---------- */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--rule-strong); }
::-webkit-scrollbar-thumb:hover { background: var(--fg-faint); }

/* ---------- Theme picker (in the topbar) ---------- */
.theme-picker {
  background: var(--bg-input);
  border: 1px solid var(--rule);
  color: var(--fg-dim);
  font-family: var(--mono);
  font-size: 11px;
  padding: 4px 6px;
  outline: none;
  cursor: pointer;
}
.theme-picker:hover { border-color: var(--rule-strong); color: var(--fg); }

/* ---------- Discussion title as link ---------- */
.chat-header .title a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px dotted transparent;
  transition: border-color 100ms;
}
.chat-header .title a:hover { border-bottom-color: var(--accent); }
.chat-header .title .open-icon {
  font-size: 0.6em;
  color: var(--fg-faint);
  margin-left: 8px;
  vertical-align: middle;
  transition: color 100ms;
}
.chat-header .title a:hover .open-icon { color: var(--accent); }

/* ---------- Participants rail ---------- */
aside.participants {
  border-left: 1px solid var(--rule);
  background: var(--bg);
  overflow-y: auto;
  padding: 14px 16px;
}
.participants-header {
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--fg-faint);
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.participants-header .count { color: var(--fg-dim); }
.participant {
  display: grid;
  grid-template-columns: 24px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 6px 0;
  border-top: 1px solid var(--rule);
}
.participant:first-of-type { border-top: none; }
.participant .avatar {
  width: 24px;
  height: 24px;
  border: 1px solid var(--rule-strong);
  background: var(--bg-elev);
  overflow: hidden;
}
.participant .avatar img { width: 100%; height: 100%; display: block; }
.participant .login {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--fg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.participant.bot .login { color: var(--bot); }
.participant.me .login { color: var(--me); }
.participant .count {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--fg-faint);
}
