:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1c1d21;
  --muted: #6b6e76;
  --border: #e3e1dc;
  --accent: #2f5bea;
  --accent-text: #ffffff;
  --agree: #0f8b7a;
  --agree-bg: #e2f4f1;
  --disagree: #b5561c;
  --disagree-bg: #fbeadf;
  --live: #d92d4b;
  --error: #c0262d;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131417;
    --surface: #1c1e22;
    --text: #ececef;
    --muted: #9a9ca5;
    --border: #2d3036;
    --accent: #6d8cff;
    --accent-text: #0d0f14;
    --agree: #47c9b4;
    --agree-bg: #13302c;
    --disagree: #f0995f;
    --disagree-bg: #3a2416;
    --live: #ff5c78;
    --error: #ff6b70;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 800; font-size: 20px; letter-spacing: -0.02em; }
.brand span { color: var(--accent); }

input {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; min-width: 0;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn {
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-text);
  border-radius: 8px; padding: 9px 16px;
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-agree { background: var(--agree); border-color: var(--agree); color: #fff; }
.btn-disagree { background: var(--disagree); border-color: var(--disagree); color: #fff; }

.view { max-width: 1100px; margin: 0 auto; padding: 24px; }
.intro h1 { font-size: clamp(24px, 4vw, 34px); line-height: 1.15; letter-spacing: -0.02em; margin: 8px 0 8px; }
.intro p { color: var(--muted); max-width: 700px; margin: 0 0 20px; }
.muted { color: var(--muted); }
.error { color: var(--error); min-height: 1.5em; margin: 6px 0; font-size: 14px; }

.new-topic { display: flex; gap: 8px; }
.new-topic input { flex: 1; }

.topic-list { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 12px; }
.topic {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; display: flex; align-items: center; gap: 16px;
}
.topic-body { flex: 1; min-width: 0; }
.topic-statement { font-weight: 600; font-size: 17px; margin: 0 0 4px; }
.topic-meta { font-size: 13px; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; }
.waiting-pill { font-weight: 600; }
.waiting-pill.agree { color: var(--agree); }
.waiting-pill.disagree { color: var(--disagree); }
.topic-actions { display: flex; gap: 8px; }
.btn-danger { background: transparent; color: var(--error); border-color: var(--error); }
.btn-danger:hover:not(:disabled) { background: var(--error); color: #fff; filter: none; }
.admin-panel {
  border: 1px dashed var(--accent); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px;
  display: grid; gap: 10px; background: var(--surface);
}
.admin-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--accent); }
.admin-panel select { width: auto; max-width: 200px; }
.admin-panel .info, .admin-panel .error { margin: 0; }
.subject-tabs { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 12px; }
.subject-tab {
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer; border-radius: 99px; padding: 6px 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.subject-tab[aria-current="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.subject-tab .count { opacity: 0.7; font-weight: 500; margin-left: 4px; }

.overlay {
  position: fixed; inset: 0; background: rgb(0 0 0 / 0.55); z-index: 20;
  display: grid; place-items: center; padding: 16px;
}
.overlay-card {
  background: var(--surface); border-radius: var(--radius); padding: 28px;
  width: min(440px, 100%); text-align: center;
}
.overlay-card h2 { margin: 8px 0; font-size: 20px; }
.preview { width: 100%; border-radius: 8px; background: #000; margin: 12px 0; aspect-ratio: 4 / 3; object-fit: cover; transform: scaleX(-1); }
.spinner {
  width: 36px; height: 36px; margin: 0 auto; border-radius: 50%;
  border: 4px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.debate-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 16px; }
.debate-header h2 { margin: 2px 0 0; font-size: 22px; line-height: 1.25; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 700; }

.stage { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.tile {
  margin: 0; position: relative; background: #000; border-radius: var(--radius); overflow: hidden;
  aspect-ratio: 4 / 3; border: 3px solid transparent; transition: border-color 0.2s;
}
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
#localVideo { transform: scaleX(-1); }
.tile.speaking { border-color: var(--live); }
.tile figcaption {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 10px 12px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: linear-gradient(transparent, rgb(0 0 0 / 0.7)); color: #fff;
}
.who { font-weight: 700; }
.stance { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; padding: 2px 8px; border-radius: 99px; }
.stance.agree { background: var(--agree-bg); color: var(--agree); }
.stance.disagree { background: var(--disagree-bg); color: var(--disagree); }
.mic-badge { margin-left: auto; font-size: 12px; font-weight: 700; background: var(--live); color: #fff; padding: 2px 8px; border-radius: 99px; display: none; }
.tile.speaking .mic-badge { display: inline-block; }
.tile-status { position: absolute; inset: 0; display: grid; place-items: center; color: #ccc; font-size: 14px; }

.mic-panel {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; display: flex; gap: 16px; align-items: center;
}
.mic-status { flex: 1; min-width: 0; }
.mic-headline { font-weight: 700; font-size: 17px; }
.mic-sub { font-size: 14px; }
.bar { height: 6px; background: var(--border); border-radius: 99px; margin-top: 8px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--accent); transition: width 0.25s linear; }
.bar-fill.open { background: var(--agree); }
.mic-actions { display: flex; gap: 8px; }
#openMicBtn[aria-pressed="true"] { background: var(--agree); border-color: var(--agree); color: #fff; }
.mic-badge.both { background: var(--agree); }

.chat { margin-top: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 18px; }
.chat-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.chat-head h3 { margin: 0; font-size: 16px; }
.chat-head .muted { font-size: 13px; }
.chat-log { list-style: none; margin: 10px 0; padding: 0; height: 260px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.msg { font-size: 15px; overflow-wrap: anywhere; }
.msg .from { font-weight: 700; margin-right: 6px; }
.msg.system { color: var(--muted); font-size: 13px; font-style: italic; }
.msg.factcheck-request { color: var(--muted); font-size: 14px; }
.msg.ai {
  background: var(--bg); border-left: 3px solid var(--accent); border-radius: 6px; padding: 8px 12px; white-space: pre-wrap;
}
.msg.ai.failed { border-left-color: var(--error); }
.msg.ai .sources { margin: 6px 0 0; padding-left: 18px; font-size: 13px; white-space: normal; }
.msg.ai a { color: var(--accent); }
.chat-form { display: flex; gap: 8px; }
.chat-form input { flex: 1; }

/* ---------- Auth & settings ---------- */
.view.narrow { max-width: 480px; }
.view.medium { max-width: 760px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 24px; margin-bottom: 16px;
}
.card-title { font-size: 24px; line-height: 1.2; letter-spacing: -0.02em; margin: 0 0 6px; }
.page-title { font-size: 26px; letter-spacing: -0.02em; margin: 8px 0 16px; }
.section-title { font-size: 17px; margin: 0 0 12px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.stack label input { font-weight: 400; }
.hint { font-weight: 400; font-size: 13px; color: var(--muted); margin: 0; }
.info { color: var(--agree); min-height: 0; margin: 12px 0 0; font-size: 14px; }
.info:empty, .error:empty { display: none; }
.small-link { color: var(--accent); font-size: 14px; text-decoration: none; align-self: flex-start; }
.small-link:hover { text-decoration: underline; }
.back-link { display: inline-block; margin-top: 4px; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 10px; margin: 18px 0; }
.tab {
  flex: 1; font: inherit; font-weight: 600; font-size: 14px; border: 0; border-radius: 8px; padding: 8px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.tab[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }
.divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 13px; margin: 18px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.google-btn { display: flex; justify-content: center; min-height: 44px; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; margin: 0; font-size: 15px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
#resendBtn { margin-top: 10px; }

.user-nav { display: flex; align-items: center; gap: 14px; font-size: 14px; }
.user-name { font-weight: 700; }
.nav-link { font: inherit; color: var(--accent); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: none; white-space: nowrap; }
.nav-link:hover { text-decoration: underline; }

/* ---------- Leaderboard & profile ---------- */
textarea, select {
  font: inherit; font-weight: 400; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; width: 100%;
}
textarea { resize: vertical; min-height: 72px; }
textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.leaderboard-cta { display: inline-block; color: var(--accent); font-weight: 600; text-decoration: none; margin-bottom: 20px; }
.leaderboard-cta:hover { text-decoration: underline; }
.leaderboard { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.lb-row {
  display: grid; grid-template-columns: 48px 1fr auto; gap: 14px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px;
}
.lb-row.you { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.lb-rank { font-size: 20px; font-weight: 800; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.lb-row.top .lb-rank { color: var(--text); }
.lb-main { min-width: 0; }
.lb-name { font-weight: 700; display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.lb-you { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--accent); }
.lb-meta { font-size: 13px; color: var(--muted); }
.lb-bio { font-size: 14px; margin-top: 2px; overflow-wrap: anywhere; }
.lb-count { text-align: right; }
.lb-count strong { display: block; font-size: 22px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.lb-count span { font-size: 12px; color: var(--muted); }
.my-rank { background: var(--surface); border: 1px dashed var(--accent); border-radius: var(--radius); padding: 10px 16px; margin-bottom: 12px; font-size: 14px; }
.where { font-size: 13px; opacity: 0.9; }
.language-note { font-size: 14px; color: var(--muted); margin: -8px 0 12px; }
.language-note a { color: var(--accent); }

@media (max-width: 720px) {
  .topbar { padding: 10px 16px; }
  .user-nav { gap: 10px; }
  .user-name { display: none; }
  .lb-row { grid-template-columns: 32px 1fr auto; gap: 10px; padding: 10px 12px; }
  .card { padding: 18px; }
  .view { padding: 16px; }
  .new-topic { flex-direction: column; }
  .topic { flex-direction: column; align-items: stretch; }
  .topic-actions .btn { flex: 1; }
  .stage { grid-template-columns: 1fr 1fr; gap: 8px; }
  .mic-panel { flex-direction: column; align-items: stretch; }
  .mic-actions .btn { flex: 1; }
  .chat-form { flex-wrap: wrap; }
  .chat-form input { flex-basis: 100%; }
  .chat-form .btn { flex: 1; }
}
