:root {
  --surface: #fdf8f6;
  --surface-1: #f6f1ef;
  --surface-2: #f0eae8;
  --surface-hi: #ffffff;
  --on-surface: #1f1b1a;
  --on-surface-2: #51453f;
  --outline: #d6c3bb;
  --primary: #8c4a2f;
  --on-primary: #ffffff;
  --primary-container: #ffdbcc;
  --on-primary-container: #3a1200;
  --danger: #ba1a1a;
  --bubble-in: #f0eae8;
  --bubble-out: #ffdbcc;
  --shadow: 0 1px 2px rgba(0, 0, 0, .12), 0 2px 6px rgba(0, 0, 0, .08);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --surface: #1a1210;
    --surface-1: #231a17;
    --surface-2: #2c211e;
    --surface-hi: #342825;
    --on-surface: #f1dfd9;
    --on-surface-2: #d6c3bb;
    --outline: #56433d;
    --primary: #ffb596;
    --on-primary: #532200;
    --primary-container: #723919;
    --on-primary-container: #ffdbcc;
    --danger: #ffb4ab;
    --bubble-in: #2c211e;
    --bubble-out: #723919;
  }
}
:root[data-theme="dark"] {
  --surface: #1a1210;
  --surface-1: #231a17;
  --surface-2: #2c211e;
  --surface-hi: #342825;
  --on-surface: #f1dfd9;
  --on-surface-2: #d6c3bb;
  --outline: #56433d;
  --primary: #ffb596;
  --on-primary: #532200;
  --primary-container: #723919;
  --on-primary-container: #ffdbcc;
  --danger: #ffb4ab;
  --bubble-in: #2c211e;
  --bubble-out: #723919;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--surface);
  color: var(--on-surface);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

h1, h2 { margin: 0; font-weight: 500; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
.sub { margin: 0; color: var(--on-surface-2); font-size: 13px; }

/* top bar */
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--surface-1); border-bottom: 1px solid var(--outline);
  position: sticky; top: 0; z-index: 2;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  background: var(--primary-container); color: var(--on-primary-container); font-weight: 600;
}
.top-actions { display: flex; align-items: center; gap: 8px; }
.live { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--on-surface-2); }
.live.on { background: var(--primary-container); color: var(--on-primary-container); }

/* buttons */
.btn {
  font: inherit; border: 0; border-radius: 999px; padding: 9px 18px; cursor: pointer;
  min-height: 40px; transition: box-shadow .15s, background .15s;
}
.btn.filled { background: var(--primary); color: var(--on-primary); box-shadow: var(--shadow); }
.btn.filled:hover { filter: brightness(1.08); }
.btn.text { background: transparent; color: var(--primary); }
.btn.text:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn:focus-visible, .seg:focus-visible, input:focus-visible, textarea:focus-visible, .conv:focus-visible {
  outline: 3px solid var(--primary); outline-offset: 2px;
}
.btn:disabled { opacity: .5; cursor: default; }

/* sign in */
.signin { display: grid; place-items: center; padding: 48px 16px; }
.card {
  background: var(--surface-1); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow);
  width: min(440px, 100%);
}
.signin-card p { color: var(--on-surface-2); margin: 8px 0 16px; }
.code-form { display: grid; gap: 8px; margin-top: 16px; }
.code-form label { font-size: 13px; color: var(--on-surface-2); }
.code-form input {
  font: inherit; font-size: 22px; letter-spacing: .3em; text-align: center;
  padding: 10px; border-radius: 12px; border: 1px solid var(--outline);
  background: var(--surface-hi); color: var(--on-surface);
}
.code-form .btn { justify-self: stretch; }
.msg { min-height: 1.4em; color: var(--danger); font-size: 13px; }

/* app layout */
.app {
  display: grid; grid-template-columns: minmax(260px, 340px) 1fr;
  height: calc(100vh - 65px); min-height: 0;
}
.list-pane { border-right: 1px solid var(--outline); display: flex; flex-direction: column; min-height: 0; }
.search { padding: 12px; }
.search input {
  width: 100%; font: inherit; padding: 10px 14px; border-radius: 999px;
  border: 1px solid var(--outline); background: var(--surface-hi); color: var(--on-surface);
}
.hint { margin: 0 12px 8px; font-size: 13px; color: var(--on-surface-2); }
.conv-list { list-style: none; margin: 0; padding: 0 8px 12px; overflow-y: auto; flex: 1; }
.conv {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; width: 100%; text-align: left;
  padding: 10px 12px; border-radius: 14px; border: 0; background: transparent; color: inherit; cursor: pointer; font: inherit;
}
.conv:hover { background: var(--surface-2); }
.conv[aria-current="true"] { background: var(--primary-container); color: var(--on-primary-container); }
.conv .n { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .t { font-size: 12px; color: var(--on-surface-2); }
.conv .p { grid-column: 1 / -1; font-size: 13px; color: var(--on-surface-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .badge { background: var(--primary); color: var(--on-primary); border-radius: 999px; font-size: 12px; padding: 0 7px; min-width: 22px; text-align: center; }
.conv .chip { font-size: 11px; color: var(--on-surface-2); }
.empty { padding: 16px; color: var(--on-surface-2); font-size: 14px; }
.scan-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 0 8px 8px; }
.scan-row .hint { margin: 0; padding: 0; }

/* thread */
.thread-pane { display: flex; flex-direction: column; min-height: 0; padding: 0 20px 16px; }
.no-sel { margin: auto; color: var(--on-surface-2); }
#sel { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.thread-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 0 8px; flex-wrap: wrap; }
.mode { display: inline-flex; border: 1px solid var(--outline); border-radius: 999px; overflow: hidden; }
.seg { font: inherit; border: 0; background: transparent; color: var(--on-surface); padding: 8px 16px; cursor: pointer; }
.seg[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }
.mode-note { margin: 0 0 8px; font-size: 13px; color: var(--on-surface-2); }
.messages {
  flex: 1; min-height: 200px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
  padding: 8px 4px; background: var(--surface-1); border-radius: var(--radius);
}
.msg-row { display: flex; }
.msg-row.out { justify-content: flex-end; }
.bubble {
  max-width: min(640px, 82%); padding: 8px 12px; border-radius: 16px; background: var(--bubble-in);
  white-space: pre-wrap; word-wrap: break-word;
}
.msg-row.out .bubble { background: var(--bubble-out); }
.meta { font-size: 11px; color: var(--on-surface-2); margin-top: 2px; }
.att { font-size: 13px; }
.att a { color: var(--primary); }
.more { align-self: center; margin-top: 6px; }
.composer { display: grid; gap: 8px; margin-top: 12px; }
.composer textarea {
  font: inherit; resize: vertical; padding: 10px 14px; border-radius: 16px;
  border: 1px solid var(--outline); background: var(--surface-hi); color: var(--on-surface);
}
.composer-row { display: flex; align-items: center; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.count { margin-right: auto; font-size: 12px; color: var(--on-surface-2); }
.footnote { font-size: 12px; color: var(--on-surface-2); margin: 10px 0 0; }

/* dialog, log, toast */
.dlg { border: 0; border-radius: 24px; padding: 0; width: min(720px, 94vw); background: var(--surface-1); color: var(--on-surface); box-shadow: var(--shadow); }
.dlg::backdrop { background: rgba(0, 0, 0, .4); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; margin: 0; }
.log { list-style: none; margin: 0; padding: 0 20px 20px; max-height: 60vh; overflow-y: auto; font-size: 13px; }
.log li { padding: 8px 0; border-top: 1px solid var(--outline); display: grid; grid-template-columns: 150px 1fr; gap: 8px; }
.log .when { color: var(--on-surface-2); }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); max-width: min(520px, 92vw);
  background: var(--surface-hi); color: var(--on-surface); padding: 12px 18px; border-radius: 12px; box-shadow: var(--shadow); font-size: 14px;
}

/* narrow screens */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; height: auto; }
  .list-pane { border-right: 0; border-bottom: 1px solid var(--outline); max-height: 40vh; }
  .thread-pane { padding: 0 12px 12px; }
  .messages { min-height: 50vh; max-height: 60vh; flex: none; }
  .top { padding: 10px 14px; }
  .brand p, .sub { font-size: 12px; }
}
