:root {
  --bg: #fff; --side: #fff; --text: #000; --muted: #8a8a8e; --line: #e5e5ea; --fill: #f2f2f7;
  --blue: #0a84ff; --bubble-in: #e9e9eb; --bubble-in-text: #000; --bubble-out: #0a84ff;
  --urgent: #ff3b30; --today: #ff9500; --week: #34c759; --low: #8e8e93;
  --sel: #e9e9eb; --thread-bg: #fff;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #000; --side: #000; --text: #fff; --muted: #8d8d93; --line: #2c2c2e; --fill: #1c1c1e;
    --bubble-in: #26252a; --bubble-in-text: #fff; --sel: #2c2c2e; --thread-bg: #000; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.error { color: var(--urgent); }
[hidden] { display: none !important; }

/* ---- shell: list | thread ---- */
.shell { display: flex; height: 100dvh; }
.side { display: flex; flex-direction: column; width: 100%; background: var(--side); }
.thread { display: none; flex-direction: column; flex: 1; background: var(--thread-bg); min-width: 0; }
.shell.open .side { display: none; }
.shell.open .thread { display: flex; }
@media (min-width: 760px) {
  .side { width: 360px; flex: none; border-right: 1px solid var(--line); }
  .thread, .shell.open .thread { display: flex; }
  .shell.open .side { display: flex; }
  .back { display: none; }
}

/* ---- list ---- */
.side-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 6px;
}
.side-title h1 { font-size: 32px; font-weight: 700; margin: 0; letter-spacing: -0.02em; }
.synced { font-size: 12px; color: var(--muted); }
.side-tools { display: flex; gap: 2px; }
.icon-btn { border: 0; background: none; color: var(--blue); padding: 6px; border-radius: 8px; cursor: pointer; display: inline-flex; }
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.filters { display: flex; gap: 2px; background: var(--fill); padding: 2px; border-radius: 9px; margin: 4px 16px 6px; }
.filters button { flex: 1; border: 0; background: none; padding: 5px; border-radius: 7px; font-size: 13px; font-weight: 600; cursor: pointer; }
.filters button.active { background: var(--side); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
@media (prefers-color-scheme: dark) { .filters button.active { background: #636366; } }
.count { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--blue); color: #fff; font-size: 11px; line-height: 18px; margin-left: 2px; }
.count:empty { display: none; }

.list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: env(safe-area-inset-bottom); }
.section-head {
  display: flex; align-items: center; gap: 6px; padding: 14px 16px 4px; font-size: 13px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
.section-head .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--low); }
.section-head.urgent .dot { background: var(--urgent); }
.section-head.today .dot { background: var(--today); }
.section-head.week .dot { background: var(--week); }
.section-head button { margin-left: auto; border: 0; background: none; color: var(--blue); font-size: 13px; font-weight: 600; cursor: pointer; }

/* Rows like Messages: the divider runs under the text, not the avatar, and
   the selected row is a rounded inset highlight with the dividers hidden. */
.row { display: flex; gap: 12px; padding: 10px 8px 0 16px; margin: 0 8px; border-radius: 10px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.row.selected { background: var(--sel); }
.row.selected .row-body, .row:has(+ .row.selected) .row-body, .row:last-child .row-body { border-bottom-color: transparent; }
@media (min-width: 760px) {
  .row:hover:not(.selected) { background: var(--fill); }
  .row:hover:not(.selected) .row-body, .row:has(+ .row:hover) .row-body { border-bottom-color: transparent; }
  .row.selected { background: var(--blue); color: #fff; }
  .row.selected .muted, .row.selected .time, .row.selected .ask { color: rgba(255,255,255,.85); }
}
.row-body { flex: 1; min-width: 0; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.row-top .name { flex: 1; font-weight: 600; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.time { font-size: 15px; color: var(--muted); flex: none; }
.row-sum { font-size: 15px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.row.needs .row-sum { color: var(--text); }
.ask { font-size: 14px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ask b { font-weight: 600; }
.avatar {
  width: 46px; height: 46px; border-radius: 50%; flex: none; display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 17px; background: #8e8e93; position: relative;
}
.avatar.small { width: 36px; height: 36px; font-size: 14px; }
.avatar .badge { position: absolute; right: -2px; top: -2px; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--side); }
.badge.urgent { background: var(--urgent); } .badge.today { background: var(--today); } .badge.week { background: var(--week); } .badge.low { background: var(--low); }
.chip { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 999px; background: var(--fill); color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.chip.urgent { background: var(--urgent); color: #fff; } .chip.today { background: var(--today); color: #fff; } .chip.week { background: var(--week); color: #fff; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; font-size: 15px; }

/* ---- thread ---- */
.thread-empty { flex: 1; display: grid; place-items: center; color: var(--muted); }
.shell.open .thread-empty { display: none; }
.thread-head {
  display: flex; align-items: center; gap: 4px; padding: calc(6px + env(safe-area-inset-top)) 8px 6px;
  border-bottom: 1px solid var(--line); background: var(--thread-bg); position: sticky; top: 0; z-index: 2;
}
.thread-who { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; }
.thread-who .who-name { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-who .who-sub { font-size: 12px; color: var(--muted); }

.brief { margin: 0 12px 6px; padding: 10px 12px; background: var(--fill); border-radius: 14px; font-size: 14px; flex: none; }
.brief-top { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.brief-top .spacer { flex: 1; }
.brief .sum { margin: 0; }
.brief .ask { white-space: normal; margin-top: 4px; font-size: 14px; color: var(--text); }
.brief-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.pill { border: 0; background: var(--side); color: var(--blue); padding: 5px 11px; border-radius: 999px; font-size: 13px; font-weight: 600; cursor: pointer; }
@media (prefers-color-scheme: dark) { .pill { background: #2c2c2e; } }

.messages { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 12px 28px; display: flex; flex-direction: column; gap: 2px; overscroll-behavior: contain; }
.day { text-align: center; font-size: 12px; color: var(--muted); margin: 12px 0 6px; }
.msg { display: flex; flex-direction: column; max-width: 78%; align-self: flex-start; }
.msg.me { align-self: flex-end; align-items: flex-end; }
.msg .from { font-size: 12px; color: var(--muted); margin: 6px 0 1px 12px; }
.bubble {
  padding: 7px 12px; border-radius: 18px; background: var(--bubble-in); color: var(--bubble-in-text);
  white-space: pre-wrap; overflow-wrap: anywhere; font-size: 17px; line-height: 1.3;
}
.msg.me .bubble { background: var(--bubble-out); color: #fff; }
.msg.pending .bubble { opacity: .6; }
.msg img.photo { max-width: 100%; max-height: 320px; border-radius: 14px; display: block; margin-top: 2px; }
.file { display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 14px; background: var(--bubble-in); color: var(--bubble-in-text); font-size: 15px; text-decoration: none; margin-top: 2px; }
.msg.me .file { background: var(--bubble-out); color: #fff; }
.reactions { font-size: 12px; color: var(--muted); margin: 1px 10px 4px; }
.msg.me .reactions { text-align: right; }
.status { font-size: 11px; color: var(--muted); margin: 2px 10px; }

.composer { padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); background: var(--thread-bg); }
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1; resize: none; font: inherit; font-size: 17px; line-height: 22px; padding: 8px 16px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--thread-bg); color: var(--text); outline: none; overflow-y: auto;
  field-sizing: content; min-height: 40px; max-height: 196px; box-sizing: border-box; transition: height .12s ease-out;
}
.composer-row { align-items: flex-end; }
.send-btn { margin-bottom: 3px; }
.composer textarea:focus { border-color: var(--blue); }
.send-btn { width: 34px; height: 34px; border-radius: 50%; border: 0; background: var(--blue); color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; margin-bottom: 2px; }
.send-btn:disabled { background: var(--line); color: var(--muted); }
.draft-note { margin: 4px 12px 0; font-size: 12px; color: var(--muted); }
.files { display: flex; gap: 8px; flex-wrap: wrap; padding: 4px 8px; }
.files:empty { display: none; }
.files .f { position: relative; }
.files img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; display: block; }
.files .fname { width: 64px; height: 64px; border-radius: 10px; background: var(--fill); font-size: 11px; padding: 6px; overflow: hidden; }
.files .x { position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: var(--text); color: var(--bg); font-size: 12px; cursor: pointer; }

/* ---- panels, dialogs, sheets ---- */
.banner { margin: 6px 12px; padding: 10px 12px; background: var(--fill); border-radius: 12px; font-size: 14px; }
.banner.warn { background: color-mix(in srgb, var(--today) 18%, var(--bg)); }
.banner.error { background: color-mix(in srgb, var(--urgent) 18%, var(--bg)); }
.banner-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.banner p { margin: 4px 0 0; }
.panel { margin: 6px 12px; padding: 12px; background: var(--fill); border-radius: 14px; font-size: 14px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; }
.panel h2 { margin: 0; font-size: 16px; }
.panel .ok { color: var(--week); font-weight: 600; }
.devices { list-style: none; padding: 0; margin: 8px 0; }
.devices li { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--line); }
.offer { text-align: center; margin-top: 8px; }
.offer img { background: #fff; border-radius: 10px; padding: 6px; }
.offer .code { font: 600 24px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .08em; margin: 6px 0; }
.link { border: 0; background: none; color: var(--blue); font: inherit; cursor: pointer; padding: 0; text-decoration: none; }
.btn { border: 0; background: var(--blue); color: #fff; padding: 8px 14px; border-radius: 999px; font-weight: 600; cursor: pointer; font-size: 14px; }

.overlay { position: fixed; inset: 0; background: var(--bg); z-index: 20; display: grid; place-items: start center; padding: 40px 16px; overflow: auto; }
.dialog { width: 100%; max-width: 420px; background: var(--fill); border-radius: 16px; padding: 22px; display: grid; gap: 10px; }
.dialog h2 { margin: 0; }
.dialog p { margin: 0; color: var(--muted); font-size: 15px; }
.dialog input { font: inherit; font-size: 18px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.dialog button { font: inherit; font-weight: 600; padding: 10px; border-radius: 10px; border: 0; cursor: pointer; }
.dialog .primary { background: var(--blue); color: #fff; }
.dialog button:disabled { opacity: .6; }
.hint { background: var(--bg); border-radius: 10px; padding: 8px 10px; font-size: 14px; }

.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: flex-end; justify-content: center; z-index: 30; }
.sheet-body { background: var(--bg); width: 100%; max-width: 480px; border-radius: 16px 16px 0 0; padding: 12px 12px calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 6px; }
.sheet-title { margin: 4px 0 6px; text-align: center; color: var(--muted); font-size: 14px; }
.sheet-body button, .sheet-body input { font: inherit; font-size: 17px; padding: 13px; border: 0; border-radius: 12px; background: var(--fill); color: var(--blue); cursor: pointer; }
.sheet-body input { color: var(--text); outline: none; }
.sheet-body .cancel { color: var(--text); font-weight: 600; }
.sheet-body .danger { color: var(--urgent); }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 14px; border-radius: 12px; display: flex; gap: 14px; align-items: center; z-index: 40; font-size: 14px; max-width: 90vw;
}
.toast button { border: 0; background: none; color: #6cb6ff; font: inherit; font-weight: 600; cursor: pointer; }

.unread-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--blue); flex: none; margin-right: 2px; }
.row-top .name.bold { font-weight: 700; }
.row.selected .unread-dot { background: #fff; }

/* Desktop: Messages-like density (the phone sizes are too big side by side with Messages.app). */
@media (min-width: 760px) {
  body { font-size: 14px; }
  .side-title h1 { font-size: 22px; }
  .side-head { padding-top: 10px; }
  .row-top .name { font-size: 13px; }
  .time { font-size: 12px; }
  .row-sum { font-size: 12.5px; }
  .ask { font-size: 12px; }
  .avatar { width: 38px; height: 38px; font-size: 14px; }
  .section-head { font-size: 11px; padding: 10px 16px 2px; }
  .filters button { font-size: 12px; padding: 4px; }
  .bubble { font-size: 14px; padding: 6px 11px; }
  .composer textarea { font-size: 14px; line-height: 20px; padding: 7px 14px; min-height: 36px; max-height: 180px; }
  .brief { font-size: 13px; }
  .brief .ask { font-size: 13px; }
  .pill { font-size: 12px; padding: 4px 10px; }
  .thread-who .who-name { font-size: 14px; }
  .day, .reactions, .status { font-size: 11px; }
  .file { font-size: 13px; }
  .draft-note { font-size: 11px; }
}

.filters.many { flex-wrap: wrap; }
.filters.many button { flex: 1 1 30%; }

/* Thin overlay scrollbars, like macOS. */
.list, .messages, .composer textarea, .side { scrollbar-width: thin; scrollbar-color: color-mix(in srgb, var(--muted) 45%, transparent) transparent; }
.list::-webkit-scrollbar, .messages::-webkit-scrollbar, .composer textarea::-webkit-scrollbar { width: 6px; }
.list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb, .composer textarea::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--muted) 45%, transparent); border-radius: 3px; }
.list::-webkit-scrollbar-track, .messages::-webkit-scrollbar-track, .composer textarea::-webkit-scrollbar-track { background: transparent; }
.section-head { margin: 0 8px; }

.section-head.checking .dot { background: var(--muted); opacity: .5; }
.chip.checking { background: var(--line); color: var(--muted); }

/* Filter pills under the title: one row, scrolls sideways, no scrollbar. */
.pills { display: flex; gap: 8px; overflow-x: auto; padding: 2px 16px 8px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.pills::-webkit-scrollbar { display: none; }
.pill-f { flex: none; display: inline-flex; align-items: center; gap: 6px; border: 0; border-radius: 999px; padding: 6px 12px; background: var(--fill); color: var(--text); font-size: 14px; font-weight: 600; cursor: pointer; }
.pill-f .n { font-size: 12px; font-weight: 700; padding: 0 6px; border-radius: 999px; background: rgba(127,127,127,.18); }
.pill-f.active { background: var(--blue); color: #fff; }
.pill-f.active .n { background: rgba(255,255,255,.25); }
@media (min-width: 760px) { .pill-f { font-size: 13px; padding: 5px 11px; } }

/* Pairing is a focused dialog: devices above, a fresh QR code below. */
.phones-overlay { position: fixed; inset: 0; z-index: 25; display: flex; align-items: center; justify-content: center; padding: 24px; background: rgba(0,0,0,.3); }
.phones-dialog { width: min(400px, 100%); max-height: calc(100dvh - 48px); overflow-y: auto; padding: 18px; border: 1px solid var(--line); border-radius: 18px; background: var(--bg); box-shadow: 0 20px 60px rgba(0,0,0,.25); font-size: 14px; }
.phones-dialog h2 { margin: 0; font-size: 18px; }
.phones-intro { margin: 5px 0 14px; font-size: 13px; }
.phones-dialog .relay-state { margin: 0 0 12px; font-size: 12px; }
.phones-dialog .ok { color: var(--week); }
.phones-label { margin: 12px 0 4px; color: var(--muted); font-size: 12px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
.phones-label.add-device { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.devices-empty { margin: 8px 0; }
.offer { margin-top: 8px; }
.offer p { margin: 7px 0; }
.offer-expiry { font-size: 12px; }
@media (max-width: 759px) {
  .phones-overlay { align-items: flex-end; padding: 12px; }
  .phones-dialog { max-height: calc(100dvh - 24px); }
}
