:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --text: #1c1e21;
  --muted: #65676b;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --border: #dfe2e7;
  --bot-bubble: #ffffff;
  --user-bubble: #2563eb;
  --warning: #b42318;
  --danger: #ef4444;
  --like: #e0245e;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 16px; line-height: 1.45; -webkit-text-size-adjust: 100%; touch-action: manipulation; }
button { font-family: inherit; color: inherit; }

/* ---------- Study pages: consent, intro, questionnaires ---------- */
.page { max-width: 640px; margin: 0 auto; padding: 24px 16px 48px; }
.page h1 { font-size: 1.4rem; margin: 0 0 16px; }
.actions { display: flex; flex-direction: column; gap: 10px; margin-top: 24px; }

.button {
  display: block; width: 100%; padding: 14px 16px; border-radius: var(--radius);
  border: 1px solid var(--primary); font: inherit; font-weight: 600; text-align: center; text-decoration: none;
}
.button.primary { background: var(--primary); color: var(--primary-text); }
.button.secondary { background: transparent; color: var(--primary); }

.question-section { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.question-section h2 { font-size: 1.1rem; margin: 0 0 4px; }
.instruction { color: var(--muted); margin: 0 0 12px; }
.question { padding: 12px 8px; border-radius: 10px; border: 2px solid transparent; }
.question.missing { border-color: var(--warning); }
.question-text { margin: 0 0 10px; display: block; }
.warning { color: var(--warning); font-weight: 600; }
.scale-labels { display: flex; justify-content: space-between; gap: 8px; font-size: 0.8rem; color: var(--muted); margin-top: 6px; }
.slider { width: 100%; height: 32px; accent-color: var(--primary); }
.slider.untouched { accent-color: #9aa0a6; opacity: 0.7; }
.likert { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.likert-option { position: relative; display: block; }
.likert-option input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; }
.likert-option span {
  display: flex; align-items: center; justify-content: center; height: 44px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.likert-option input:checked + span { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
textarea { width: 100%; font: inherit; padding: 10px; border: 1px solid var(--border); border-radius: 10px; margin-top: 8px; }

/* ---------- Task layout: the app fills the screen; the typing dock sits at the bottom ---------- */
body.task-mode { height: 100dvh; overflow: hidden; }
body.task-mode #app { height: 100%; }
.task { display: flex; flex-direction: column; height: 100%; }
.scenario { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.screen-host { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.screen-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }

/* The typing dock and the tab bar are alternatives: detail screens (chat, post) hide the tab bar and show the dock. */
.surface { display: none; flex: none; padding-bottom: env(safe-area-inset-bottom); background: var(--surface); border-top: 1px solid var(--border); }
.task:has(.screen-detail) .surface { display: block; }
.scenario:has(.screen-detail) .tab-bar { display: none; }

/* ---------- App chrome ---------- */
.app-bar { flex: none; display: flex; align-items: center; gap: 8px; min-height: 52px; padding: 6px 10px; background: var(--surface); border-bottom: 1px solid var(--border); }
.logo { font-weight: 800; font-size: 1.35rem; letter-spacing: -0.03em; color: var(--primary); }
.screen-title { font-weight: 700; font-size: 1.1rem; }
.app-bar-actions { margin-left: auto; display: flex; }
.icon-button { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; border: 0; border-radius: 50%; background: transparent; }
.icon { display: block; }

.tab-bar { flex: none; display: flex; background: var(--surface); border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); }
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; border: 0; background: transparent; color: var(--muted); }
.tab.selected { color: var(--primary); }
.tab-icon { position: relative; display: inline-flex; }
.tab-label { font-size: 0.65rem; font-weight: 600; }
.badge {
  display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 999px; background: var(--danger); color: #fff; font-size: 0.72rem; font-weight: 700;
}
.tab-badge { position: absolute; top: -6px; right: -12px; min-width: 17px; height: 17px; padding: 0 4px; font-size: 0.65rem; }

/* Generated avatars */
.avatar-pic { position: relative; display: inline-block; flex: none; border-radius: 50%; }
.avatar-clip { position: relative; display: block; width: 100%; height: 100%; border-radius: 50%; overflow: hidden; }
.avatar-clip svg { display: block; }
.avatar-photo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.online-dot { position: absolute; right: -1px; bottom: -1px; width: 28%; height: 28%; border-radius: 50%; background: #22c55e; border: 2px solid var(--surface); }

/* ---------- Feed and posts ---------- */
.feed { display: flex; flex-direction: column; gap: 8px; padding-bottom: 8px; }
.post-card { background: var(--surface); padding: 12px 12px 2px; }
.post-header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.post-author { display: flex; flex-direction: column; flex: 1; min-width: 0; line-height: 1.25; }
.post-handle { color: var(--muted); font-size: 0.8rem; }
.post-text { margin: 0 0 10px; font-size: 0.92rem; overflow-wrap: anywhere; }
.post-body.tappable { cursor: pointer; }
.post-image { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 12px; background: var(--border); }
.post-actions { display: flex; align-items: center; padding: 4px 0; }
.post-action { display: inline-flex; align-items: center; gap: 6px; margin-right: 10px; padding: 6px 4px 6px 0; border: 0; background: transparent; color: var(--muted); font-size: 0.85rem; }
.post-action.liked { color: var(--like); }
.post-action.liked .icon { fill: var(--like); }
.spacer { flex: 1; }

.comments-title { margin: 14px 12px 8px; font-size: 0.95rem; }
.comments { background: var(--surface); padding: 4px 0; }
.comment { display: flex; gap: 10px; padding: 10px 12px; }
.comment-main { flex: 1; min-width: 0; }
.comment-head { display: flex; gap: 8px; align-items: baseline; font-size: 0.85rem; }
.comment-text { margin: 2px 0 4px; font-size: 0.88rem; overflow-wrap: anywhere; }
.comment-actions { display: flex; gap: 14px; }
.link-button { padding: 0; border: 0; background: transparent; color: var(--muted); font-size: 0.78rem; font-weight: 600; }
.comment-likes { display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--muted); font-size: 0.72rem; }

/* Shared article preview */
.article-card { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--surface); color: var(--text); text-align: left; }
.article-image { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: cover; background: var(--border); }
.article-source { font-size: 0.72rem; color: var(--muted); padding: 8px 10px 0; letter-spacing: 0.02em; }
.article-headline { font-weight: 700; font-size: 0.88rem; padding: 4px 10px; }
.article-summary { font-size: 0.8rem; color: var(--muted); padding: 0 10px 10px; }

/* ---------- Messages ---------- */
.conversation { display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 14px; border: 0; border-bottom: 1px solid var(--border); background: var(--surface); text-align: left; }
.conversation-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.conversation-preview { color: var(--muted); font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversation.unread .conversation-preview { color: var(--text); font-weight: 600; }
.conversation-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.conversation-time { color: var(--muted); font-size: 0.75rem; }

.notification { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--surface); font-size: 0.9rem; }
.notification-text { flex: 1; }

.profile { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 24px 16px; background: var(--surface); }
.profile h2 { margin: 8px 0 0; }
.profile-bio { margin: 4px 0; color: var(--muted); text-align: center; }
.profile-stats { display: flex; gap: 30px; margin: 10px 0 14px; }
.profile-stat { display: flex; flex-direction: column; align-items: center; font-size: 0.8rem; color: var(--muted); }
.profile-stat strong { font-size: 1.1rem; color: var(--text); }

/* ---------- Chat thread ---------- */
.thread-header { gap: 10px; }
.thread-title { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.thread-title span { font-size: 0.72rem; color: #16a34a; }
.thread-body { display: flex; flex-direction: column; gap: 6px; padding: 12px; }
.messages { display: flex; flex-direction: column; gap: 6px; }
.day-separator { align-self: center; margin-bottom: 4px; padding: 2px 10px; border-radius: 10px; background: #e1e4ea; color: var(--muted); font-size: 0.72rem; }
.bubble { max-width: 82%; padding: 7px 11px 5px; border-radius: 18px; overflow-wrap: anywhere; font-size: 0.9rem; line-height: 1.35; }
.bubble.bot { align-self: flex-start; background: var(--bot-bubble); border-bottom-left-radius: 4px; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06); }
.bubble.user { align-self: flex-end; background: var(--user-bubble); color: var(--primary-text); border-bottom-right-radius: 4px; }
/* A shared link is shown as its own card: no bubble around it, only the time underneath. */
.bubble.card { max-width: 86%; padding: 0; background: none; box-shadow: none; border-radius: 0; }
.bubble.card .bubble-meta { padding: 2px 4px 0; }
.bubble-meta { display: block; margin-top: 2px; text-align: right; font-size: 0.65rem; opacity: 0.6; }
.ticks { margin-left: 4px; letter-spacing: -2px; }
.bubble.typing { display: flex; gap: 5px; align-items: center; height: 38px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite ease-in-out; }
.dot:nth-child(2) { animation-delay: 0.15s; }
.dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* ---------- Task-complete bottom sheet ---------- */
.finish-overlay { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; background: rgba(0, 0, 0, 0.3); animation: fade-in 0.25s ease-out; }
.finish-sheet {
  width: 100%; text-align: center; background: var(--surface); border-radius: 22px 22px 0 0;
  padding: 22px 20px calc(20px + env(safe-area-inset-bottom)); box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.18);
  animation: slide-up 0.3s ease-out;
}
.finish-sheet h2 { margin: 8px 0 4px; font-size: 1.15rem; }
.finish-sheet p { margin: 0 0 18px; color: var(--muted); font-size: 0.92rem; }
.finish-check { display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; border-radius: 50%; background: #dcfce7; color: #15803d; font-size: 1.4rem; font-weight: 700; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes slide-up { from { transform: translateY(100%); } }

/* ---------- Composer, prediction bar and keyboard ---------- */
.dock.disabled { pointer-events: none; opacity: 0.55; }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 8px 10px; }
.composer-text {
  flex: 1; min-height: 40px; max-height: 96px; overflow-y: auto; padding: 9px 14px; border: 1px solid var(--border);
  border-radius: 20px; background: var(--bg); white-space: pre-wrap; overflow-wrap: anywhere; user-select: none;
}
.composer-text .placeholder { color: var(--muted); }
.composer-text:not(.focused) .caret { display: none; }
.caret { display: inline-block; width: 2px; height: 1.1em; vertical-align: text-bottom; background: var(--primary); animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.send { flex: none; display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; padding: 0 2px 2px 0; border: 0; border-radius: 50%; background: var(--primary); color: var(--primary-text); }
.send:disabled { background: var(--border); color: var(--muted); }

.keyboard-panel { background: var(--surface); }
.prediction-bar { display: flex; height: 44px; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.suggestion {
  flex: 1; min-width: 0; padding: 0 6px; border: 0; border-right: 1px solid var(--border); background: transparent;
  font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.suggestion:last-child { border-right: 0; }
.suggestion:active:not(:disabled) { background: var(--border); }

.keyboard { user-select: none; -webkit-user-select: none; }
.keyboard .simple-keyboard.hg-theme-default { background: #d1d5db; padding: 4px 3px; border-radius: 0; }
.keyboard .hg-button { height: 44px; font-size: 1.15rem; }
.keyboard .hg-button[data-skbtn="{space}"] { flex-grow: 4; }

/* ---------- Dev mode (?dev): researcher settings, opened from an app bar's ⋯ button ---------- */
.dev-menu {
  position: fixed; top: calc(env(safe-area-inset-top, 0px) + 54px); right: 8px; z-index: 30; min-width: 180px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; text-align: left;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.22);
}
.dev-menu strong { display: block; margin-bottom: 4px; font-size: 0.85rem; }
.dev-status { margin-bottom: 8px; padding: 4px 6px; border-radius: 6px; background: var(--bg); font-size: 0.72rem; }
.dev-menu label { display: flex; flex-direction: column; font-size: 0.75rem; color: var(--muted); margin-bottom: 6px; }
.dev-menu select { font: inherit; font-size: 0.9rem; margin-top: 2px; }
