:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --text: #1d1f22;
  --muted: #62666d;
  --border: #e0ded6;
  --accent: #2f6b4f;
  --accent-text: #ffffff;
  --accent-soft: #e3efe8;
  --unread: #2f6b4f;
  --bot-bg: #ffffff;
  --me-bg: #eef3f8;
  --danger: #a33a2c;
  --danger-soft: #fbe9e6;
  --info-soft: #e8f0f7;
  --closed: #7a6f5d;
  --closed-soft: #efe9de;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 20, 20, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1d2024;
    --surface-2: #24282d;
    --text: #e8e6e1;
    --muted: #9ba0a7;
    --border: #2f343a;
    --accent: #6fbf95;
    --accent-text: #0f1a14;
    --accent-soft: #1f3329;
    --unread: #7fd3a6;
    --bot-bg: #1d2024;
    --me-bg: #1c2631;
    --danger: #f08b7c;
    --danger-soft: #3a211d;
    --info-soft: #1d2a36;
    --closed: #c9b99c;
    --closed-soft: #332d23;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

h1 { font-size: 1.4rem; line-height: 1.3; margin: 0 0 0.75rem; }

.hidden { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* --- top bar --- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; }
.brand strong { display: block; font-size: 1rem; line-height: 1.2; white-space: nowrap; }
.brand small { display: block; font-size: 0.75rem; color: var(--muted); }
.brand-mark { width: 28px; height: 28px; flex: none; }

.topnav { display: flex; align-items: center; gap: 8px; }
.topnav form { margin: 0; }

.wrap { max-width: 760px; margin: 0 auto; padding: 16px 16px 48px; }

/* --- buttons and inputs --- */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 38px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 0.9rem; font-weight: 600;
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-quiet { border-color: transparent; background: transparent; color: var(--muted); }

label { display: block; font-weight: 600; font-size: 0.9rem; margin: 12px 0 6px; }

input, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text);
  font: inherit;
}
textarea { resize: vertical; min-height: 96px; }
input:focus, textarea:focus, .btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.hint { color: var(--muted); font-size: 0.8rem; font-weight: 400; }

.card {
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.flash {
  margin-bottom: 12px; padding: 10px 14px;
  border-radius: 8px; background: var(--info-soft);
}
.flash-error { background: var(--danger-soft); color: var(--danger); }

.back { margin: 0 0 8px; font-size: 0.9rem; }
.back a { text-decoration: none; }

/* --- login --- */

.login { max-width: 380px; margin: 40px auto; }
.login .btn { width: 100%; margin-top: 16px; }

/* --- tabs and thread list --- */

.tabs { display: flex; gap: 4px; margin-bottom: 12px; border-bottom: 1px solid var(--border); }
.tab {
  padding: 8px 14px; margin-bottom: -1px;
  color: var(--muted); text-decoration: none; font-weight: 600;
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.count {
  display: inline-block; min-width: 22px; padding: 0 6px; margin-left: 4px;
  border-radius: 999px; background: var(--surface-2);
  font-size: 0.75rem; text-align: center; color: var(--muted);
}

.thread-list {
  list-style: none; margin: 0; padding: 0;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow);
}
.thread-list li + li { border-top: 1px solid var(--border); }

.thread-row {
  display: grid; grid-template-columns: 10px 1fr auto; gap: 12px; align-items: start;
  padding: 12px 14px;
  color: var(--text); text-decoration: none;
}
.thread-row:hover { background: var(--surface-2); }
.dot { width: 9px; height: 9px; margin-top: 7px; border-radius: 50%; }
.unread .dot { background: var(--unread); }

.thread-main { min-width: 0; }
.thread-title { display: block; font-weight: 500; overflow-wrap: anywhere; }
.unread .thread-title { font-weight: 700; }
.thread-preview {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; color: var(--muted); font-size: 0.875rem; overflow-wrap: anywhere;
}
.who { font-weight: 600; }

.thread-meta {
  display: flex; flex-direction: column; align-items: flex-end;
  font-size: 0.75rem; color: var(--muted); white-space: nowrap;
}
.unread .thread-meta time { color: var(--unread); font-weight: 700; }

.empty { color: var(--muted); text-align: center; }

.footer-links { margin-top: 24px; text-align: center; font-size: 0.85rem; }

/* --- thread page --- */

.thread-head {
  display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 8px 16px;
  margin-bottom: 12px;
}
.thread-head h1 { margin: 0; overflow-wrap: anywhere; }
.thread-actions { display: flex; align-items: center; gap: 8px; }
.thread-actions form { display: flex; gap: 6px; margin: 0; }

.pill {
  display: inline-block; padding: 2px 10px; border-radius: 999px;
  font-size: 0.75rem; font-weight: 700;
}
.pill-open { background: var(--accent-soft); color: var(--accent); }
.pill-closed { background: var(--closed-soft); color: var(--closed); }

.messages { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 10px; }

.message {
  padding: 12px 14px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bot-bg); box-shadow: var(--shadow);
  scroll-margin-top: 72px;
}
.message-me { background: var(--me-bg); margin-left: 32px; }
.message-grokbot { margin-right: 32px; }
.message:target { outline: 2px solid var(--accent); }

.message-head { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 4px; font-size: 0.8rem; }
.author { font-weight: 700; }
.message-head time { color: var(--muted); }

.prose { overflow-wrap: anywhere; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p, .prose ul, .prose ol { margin: 0 0 0.6em; }
.prose ul, .prose ol { padding-left: 1.4em; }
.prose pre { overflow-x: auto; padding: 10px; border-radius: 6px; background: var(--surface-2); }
.prose code { font-size: 0.875em; }
.prose table { display: block; overflow-x: auto; border-collapse: collapse; margin: 0 0 0.6em; }
.prose th, .prose td { padding: 4px 8px; border: 1px solid var(--border); text-align: left; }
.prose blockquote { margin: 0 0 0.6em; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }

.reply label { margin-top: 0; }
.reply-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }

@media (max-width: 520px) {
  .brand small { display: none; }
  .message-me { margin-left: 16px; }
  .message-grokbot { margin-right: 16px; }
  .thread-head { flex-direction: column; }
  .reply-foot .hint { display: none; }
  .reply-foot .btn { width: 100%; }
}
