:root {
  --bg: #eef1f2; --surface: #ffffff; --ink: #14212b; --muted: #5d6c78; --line: #d6dde1;
  --accent: #0f6b66; --accent-ink: #ffffff;
  --bot: #6b7b88; --pending: #b3261e; --human: #b86500;
  --mine: #dff0ed; --theirs: #ffffff; --note: #fff4cf; --draft: #e8eefb;
  --sa-top: env(safe-area-inset-top, 0px); --sa-bot: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0e1519; --surface: #172229; --ink: #e6edf1; --muted: #93a3ae; --line: #2a3a44; --accent: #3cb3aa; --accent-ink: #062321;
    --bot: #8fa0ad; --pending: #ff8a80; --human: #f0a24a; --mine: #1b3a37; --theirs: #1d2b33; --note: #4a3f16; --draft: #1f2b45; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; padding-top: var(--sa-top); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.25rem; letter-spacing: -0.01em; }
h2 { font-size: 1.05rem; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 0.4rem 0.75rem; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .row:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--pending); border-color: var(--pending); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.wide { width: calc(100% - 1.5rem); margin: 0.75rem; }
.banner { background: var(--pending); color: #fff; padding: 0.6rem 1rem; text-align: center; }
.banner button { margin-left: 0.75rem; }
.app { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; height: 100%; min-height: 0; }
.list-pane { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.list-head { display: flex; justify-content: space-between; align-items: center; padding: 0.9rem 0.9rem 0.5rem; }
.head-actions { display: flex; gap: 0.4rem; }
.filters { padding: 0 0.9rem 0.6rem; display: grid; gap: 0.5rem; border-bottom: 1px solid var(--line); }
.filters input { width: 100%; padding: 0.45rem 0.6rem; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.seg { display: flex; gap: 0; }
.seg button { border-radius: 0; margin-left: -1px; flex: 1; padding: 0.3rem 0.5rem; }
.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.on { background: var(--ink); color: var(--surface); border-color: var(--ink); }
.seg.small button { font-size: 0.85rem; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
/* The left rail encodes who currently controls the conversation. */
.row { display: grid; grid-template-columns: 5px 1fr; border-bottom: 1px solid var(--line); cursor: pointer; }
.row .rail { background: var(--bot); }
.row[data-control="human_pending"] .rail { background: var(--pending); }
.row[data-control="human_active"] .rail { background: var(--human); }
.row .body { padding: 0.65rem 0.8rem; min-width: 0; }
.row.sel { background: var(--bg); }
.row .top { display: flex; justify-content: space-between; gap: 0.5rem; }
.row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .when { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.row .prev { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .tags { display: flex; gap: 0.4rem; margin-top: 0.25rem; font-size: 0.78rem; color: var(--muted); flex-wrap: wrap; }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 0 0.45rem; font-weight: 600; font-size: 0.78rem; }
.thread-pane { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
#thread { display: flex; flex-direction: column; min-height: 0; height: 100%; }
.empty { margin: auto; color: var(--muted); }
.thread-head { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem 1rem; background: var(--surface); border-bottom: 1px solid var(--line); }
.thread-head .who { flex: 1; min-width: 0; }
.back { display: none; }
.meta { margin: 0; color: var(--muted); font-size: 0.85rem; }
.actions { display: flex; gap: 0.4rem; flex-wrap: wrap; justify-content: flex-end; }
.state { padding: 0.5rem 1rem; font-size: 0.88rem; background: var(--surface); border-bottom: 1px solid var(--line); border-left: 5px solid var(--bot); }
.state[data-control="human_pending"] { border-left-color: var(--pending); }
.state[data-control="human_active"] { border-left-color: var(--human); }
.older { text-align: center; padding: 0.4rem; }
.msgs { list-style: none; margin: 0; padding: 1rem; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 0.5rem; }
.msg { max-width: min(70ch, 85%); padding: 0.5rem 0.75rem; border-radius: 10px; background: var(--theirs); border: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.out { align-self: flex-end; background: var(--mine); }
.msg.note { align-self: center; background: var(--note); max-width: 90%; border-style: dashed; }
.msg .sub { display: block; color: var(--muted); font-size: 0.75rem; margin-top: 0.25rem; }
.msg.failed .sub { color: var(--pending); }
.draft { margin: 0 1rem 0.5rem; padding: 0.6rem 0.75rem; background: var(--draft); border: 1px solid var(--line); border-radius: 8px; }
.draft .label { font-weight: 600; font-size: 0.8rem; }
.draft textarea { width: 100%; margin: 0.4rem 0; padding: 0.4rem; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.draft .row2 { display: flex; gap: 0.4rem; }
.composer { padding: 0.6rem 1rem calc(0.75rem + var(--sa-bot)); background: var(--surface); border-top: 1px solid var(--line); display: grid; gap: 0.4rem; }
.composer textarea { width: 100%; padding: 0.5rem; border: 1px solid var(--line); border-radius: 8px; resize: vertical; background: var(--bg); }
.composer.note-mode textarea { background: var(--note); }
.c-row { display: flex; gap: 0.5rem; justify-content: flex-end; }
.admin-pane { position: fixed; inset: 0; background: var(--bg); overflow-y: auto; padding: calc(1rem + var(--sa-top)) 1rem 3rem; z-index: 5; }
.admin-pane section { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 1rem; margin: 1rem auto; max-width: 900px; }
.admin-pane textarea { width: 100%; min-height: 260px; font: 12.5px/1.4 ui-monospace, Menlo, Consolas, monospace; padding: 0.5rem; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; }
.admin-pane input, .admin-pane select { padding: 0.35rem 0.5rem; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.admin-pane .grid { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; margin: 0.5rem 0; }
.admin-pane ul { padding-left: 1.1rem; }
.warn { color: var(--human); }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .app[data-view="list"] .thread-pane { display: none; }
  .app[data-view="thread"] .list-pane { display: none; }
  .back { display: inline-block; }
  .thread-head { flex-wrap: wrap; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }

/* Sign-in page */
.login-body { display: grid; place-items: center; min-height: 100%; padding: 1.5rem; }
.login { width: min(100%, 26rem); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 1.5rem; display: grid; gap: 0.6rem; }
.login label { font-weight: 600; margin-top: 0.4rem; }
.login input { width: 100%; padding: 0.55rem 0.65rem; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); font-size: 1rem; }
#code { letter-spacing: 0.4em; font-size: 1.4rem; text-align: center; }
.wide-btn { width: 100%; padding: 0.6rem 0.75rem; margin-top: 0.4rem; }
.center { text-align: center; margin: 0.4rem 0 0; }
.login-msg { min-height: 1.4em; color: var(--pending); margin: 0.25rem 0 0; }
.sec-row { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; padding: 0.4rem 0; border-bottom: 1px solid var(--line); }
