:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --sidebar: #f9f9f9;
  --surface: #ffffff;
  --bubble: #f2f2f3;
  --text: #0d0d0d;
  --muted: #6b6b6f;
  --faint: #9b9ba1;
  --border: #e5e5e7;
  --hover: #ececee;
  --accent: #6d4aff;
  --accent-soft: rgba(109, 74, 255, 0.12);
  --accent-fill: rgba(109, 74, 255, 0.16);
  --send: #0f9d6a;
  --send-soft: rgba(15, 157, 106, 0.14);
  --end: #d9483b;
  --end-soft: rgba(217, 72, 59, 0.12);
  --shadow: 0 2px 18px rgba(0, 0, 0, 0.07), 0 0 0 1px var(--border);
  --radius: 26px;
  --sidebar-w: 260px;
  --thread-w: 768px;
  font-family:
    ui-sans-serif, -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.6;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --sidebar: #171717;
    --surface: #2c2c2e;
    --bubble: #303030;
    --text: #ececec;
    --muted: #a3a3a8;
    --faint: #75757b;
    --border: #3a3a3c;
    --hover: #2a2a2c;
    --accent: #9b85ff;
    --accent-soft: rgba(155, 133, 255, 0.16);
    --accent-fill: rgba(155, 133, 255, 0.22);
    --send: #3ccf91;
    --send-soft: rgba(60, 207, 145, 0.16);
    --end: #ff7a6e;
    --end-soft: rgba(255, 122, 110, 0.14);
    --shadow: 0 0 0 1px var(--border);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

button {
  font: inherit;
  color: inherit;
}

kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.72em;
  padding: 1px 5px;
  border-radius: 5px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

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

.sidebar {
  width: var(--sidebar-w);
  flex: none;
  background: var(--sidebar);
  display: flex;
  flex-direction: column;
  padding: 12px 10px;
  gap: 2px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 14px;
  font-weight: 650;
  font-size: 1.08rem;
  letter-spacing: -0.01em;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  text-align: left;
  text-decoration: none;
  color: var(--text);
  font-size: 0.92rem;
  cursor: pointer;
}

.nav-item:hover {
  background: var(--hover);
}

.nav-item.active {
  background: var(--hover);
  font-weight: 550;
}

.live-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--end);
  box-shadow: 0 0 0 0 var(--end);
  animation: live 1.8s infinite;
}

@keyframes live {
  0% {
    box-shadow: 0 0 0 0 rgba(217, 72, 59, 0.5);
  }
  70% {
    box-shadow: 0 0 0 7px rgba(217, 72, 59, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(217, 72, 59, 0);
  }
}

.section-label {
  padding: 18px 10px 6px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--faint);
}

.history {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-height: 0;
}

.history-item {
  font-size: 0.88rem;
}

.history-item,
.live-title {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.history-item {
  display: block;
}

.history-empty {
  padding: 4px 10px;
  font-size: 0.85rem;
  color: var(--faint);
}

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 12px 10px 4px;
}

.online {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
  font-weight: 550;
}

.pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--send);
}

.scrim {
  display: none;
}

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

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  min-height: 56px;
}

.icon-btn {
  display: none;
  border: 0;
  background: transparent;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
}

.icon-btn:hover,
.ghost-btn:hover {
  background: var(--hover);
}

.chat-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.chat-title.placeholder {
  color: var(--muted);
}

.chat-title.fade-in {
  animation: title-in 0.6s ease-out;
}

@keyframes title-in {
  from {
    opacity: 0;
    transform: translateY(-3px);
  }
}

.ghost-btn {
  flex: none;
  border: 1px solid var(--border);
  background: transparent;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
}

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

.thread {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.thread-inner {
  max-width: var(--thread-w);
  margin: 0 auto;
  padding: 24px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 26px;
}

.intro {
  text-align: center;
  padding: 22vh 12px 0;
}

.intro.hidden {
  display: none;
}

.intro h2 {
  margin: 0;
  font-size: 1.75rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.msg.human {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.bubble {
  max-width: min(78%, 560px);
  background: var(--bubble);
  padding: 10px 18px;
  border-radius: 22px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.caret {
  display: inline-block;
  width: 2px;
  height: 1.1em;
  margin-left: 2px;
  vertical-align: -0.18em;
  background: var(--accent);
  animation: blink 1s steps(1) infinite;
}

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

.new-token {
  display: inline-block;
  color: var(--accent);
  font-weight: 600;
  animation: pop 0.45s ease-out;
  white-space: pre-wrap;
}

@keyframes pop {
  0% {
    transform: translateY(6px) scale(0.85);
    opacity: 0;
  }
  60% {
    transform: translateY(-2px) scale(1.06);
    opacity: 1;
  }
  100% {
    transform: none;
  }
}

.msg.ai {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

.avatar {
  flex: none;
  width: 28px;
  height: 28px;
  margin-top: 2px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background:
    radial-gradient(circle at 35% 40%, var(--text) 2px, transparent 2.5px),
    radial-gradient(circle at 65% 40%, var(--text) 2px, transparent 2.5px), var(--surface);
}

.markdown {
  min-width: 0;
  flex: 1;
  overflow-wrap: anywhere;
}

.markdown > :first-child {
  margin-top: 0;
}

.markdown > :last-child {
  margin-bottom: 0;
}

.markdown p,
.markdown ul,
.markdown ol {
  margin: 0 0 0.8em;
}

.markdown pre {
  background: var(--bubble);
  padding: 12px 14px;
  border-radius: 12px;
  overflow-x: auto;
  font-size: 0.88em;
}

.markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
}

.markdown :not(pre) > code {
  background: var(--bubble);
  padding: 1px 5px;
  border-radius: 6px;
}

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

.markdown blockquote {
  margin: 0 0 0.8em;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

.stream-caret {
  display: inline-block;
  width: 9px;
  height: 9px;
  margin-left: 4px;
  border-radius: 50%;
  background: var(--text);
  animation: blink 0.9s steps(1) infinite;
}

.msg.human.typing .bubble {
  padding: 14px 18px;
}

.typing-dots {
  display: inline-flex;
  gap: 5px;
  align-items: center;
  height: 1.1em;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing-bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.thinking-dots::after {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-top: 8px;
  border-radius: 50%;
  background: var(--text);
  animation: breathe 1.2s ease-in-out infinite;
}

@keyframes breathe {
  50% {
    transform: scale(0.6);
    opacity: 0.5;
  }
}

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

.composer-wrap {
  padding: 0 16px max(12px, env(safe-area-inset-bottom));
}

.composer {
  position: relative;
  max-width: var(--thread-w);
  margin: 0 auto;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px 14px;
  overflow: hidden;
}

.timer {
  position: absolute;
  inset: 0 0 auto;
  height: 3px;
}

.timer-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 0 3px 3px 0;
}

.composer-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 0 4px 10px;
  font-size: 0.86rem;
}

.status {
  font-weight: 600;
}

.countdown {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.composer[data-mode="idle"] .timer {
  display: none;
}

.options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  min-height: 40px;
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 40px;
  padding: 0 12px 0 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  text-align: left;
  overflow: hidden;
  isolation: isolate;
  cursor: pointer;
  transition:
    border-color 0.15s,
    transform 0.1s;
}

.row:hover {
  border-color: var(--faint);
}

.row:active {
  transform: scale(0.985);
}

/* The probability bar fills the row from behind. */
.row .bar {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  z-index: -1;
  background: var(--hover);
  transition: width 0.35s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.row kbd {
  flex: none;
}

.row .token {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  white-space: pre;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row .votes {
  flex: none;
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.row .pct {
  flex: none;
  min-width: 42px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-size: 0.88rem;
  font-weight: 650;
}

.row.leading {
  border-color: var(--accent);
}

.row.leading .bar {
  background: var(--accent-fill);
}

.row.mine {
  box-shadow: 0 0 0 1px var(--accent);
}

.row.mine .token::after {
  content: " ✓";
  color: var(--accent);
}

.row.kind-end .token {
  color: var(--end);
  font-weight: 550;
}

.row.kind-end.leading {
  border-color: var(--end);
}

.row.kind-end.leading .bar {
  background: var(--end-soft);
}

/* The winner, briefly, before the next options replace it. */
.options .row.won {
  border-color: var(--send);
  cursor: default;
}

.options .row.won .bar {
  background: var(--send-soft);
}

/* A green check before the token (number badges are hidden on phones). */
.options .row.won .token::before {
  content: "✓ ";
  color: var(--send);
}

.options .row.won .token::after {
  content: none;
}

.options .row.lost {
  opacity: 0.4;
  cursor: default;
  transition: opacity 0.2s;
}

.row.skeleton {
  cursor: default;
  border-color: transparent;
  background: linear-gradient(90deg, var(--hover), var(--bubble), var(--hover));
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
}

@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}

.back-btn {
  grid-column: 1 / -1;
  justify-self: start;
  height: 40px;
  padding: 0 18px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-weight: 550;
  cursor: pointer;
}

.composer-note {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  min-height: 40px;
  padding: 0 4px;
  color: var(--muted);
}

.fineprint {
  margin: 8px 0 0;
  text-align: center;
  font-size: 0.74rem;
  color: var(--faint);
}

/* ------------------------------------------------------------ dialog */

.how-dialog {
  max-width: 480px;
  border: 0;
  border-radius: 20px;
  padding: 24px 26px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.how-dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.how-dialog h2 {
  margin: 0 0 8px;
  font-size: 1.25rem;
}

.how-dialog ol {
  padding-left: 20px;
}

.how-dialog li {
  margin-bottom: 8px;
}

.primary-btn {
  border: 0;
  border-radius: 999px;
  padding: 9px 20px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  cursor: pointer;
}

/* ------------------------------------------------------------ mobile */

@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
  }

  .sidebar-open .sidebar {
    transform: none;
  }

  .sidebar-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 10;
    background: rgba(0, 0, 0, 0.4);
  }

  .icon-btn {
    display: block;
  }

  .ghost-btn {
    display: none;
  }

  .thread-inner {
    padding: 12px 14px 28px;
  }

  .bubble {
    max-width: 88%;
  }

  .options {
    grid-template-columns: 1fr;
  }

  .row kbd {
    display: none;
  }

  .fineprint {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
