:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1c1d20;
  --muted: #6b6d74;
  --line: #e4e2dc;
  --accent: #2f6fdb;
  --mine: #e9f0fd;
  --unread: #fff7e0;
  --tg: #2a9fd6;
  --max: #7a4dff;
  --gmail: #d6453d;
  --ok: #2e9d5b;
  --warn: #d48a12;
  --err: #cf3b3b;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a; --surface: #1f2125; --text: #ececee; --muted: #9a9ca3; --line: #2e3036;
    --accent: #6c9ef5; --mine: #22324d; --unread: #3a3220; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a; --surface: #1f2125; --text: #ececee; --muted: #9a9ca3; --line: #2e3036;
  --accent: #6c9ef5; --mine: #22324d; --unread: #3a3220; color-scheme: dark;
}

* { box-sizing: border-box; }
/* Каркас приложения: заголовок и меню — обычные блоки, прокручивается только
   середина. Без position: fixed к окну — на iOS (особенно «с экрана Домой»)
   такие элементы съезжают при прокрутке и пружинящем WebView. */
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; }

.top {
  flex: none; position: relative; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 18px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { border: 0; background: none; font-size: 28px; line-height: 1; padding: 0 6px 2px 0; cursor: pointer; color: var(--accent); }
.status { display: flex; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--ok); } .dot.need_login, .dot.starting { background: var(--warn); } .dot.error { background: var(--err); }

main {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 12px max(16px, calc((100% - 760px) / 2)) 24px;
}

.tabs {
  flex: none; position: relative; z-index: 5;
  display: flex; justify-content: center; gap: 4px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabs button {
  flex: 1; max-width: 220px; border: 0; background: none; padding: 10px 6px; border-radius: 10px;
  color: var(--muted); cursor: pointer; font-weight: 500;
}
.tabs button.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
[hidden] { display: none !important; }
.pill {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; font-size: 12px; line-height: 20px;
  text-align: center; background: var(--accent); color: #fff; font-weight: 600;
}

.empty { color: var(--muted); text-align: center; padding: 48px 16px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 10px; cursor: pointer;
}
.card:active { transform: scale(.995); }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.card-title { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .line { color: var(--text); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card .line + .line { margin-top: 2px; }
.card .who { color: var(--muted); }
.card.me { border-left: 3px solid var(--accent); }
.when { color: var(--muted); font-size: 12px; white-space: nowrap; }

.src {
  font-size: 11px; font-weight: 700; letter-spacing: .02em; padding: 1px 6px; border-radius: 6px; color: #fff;
  flex: none;
}
.src.telegram { background: var(--tg); } .src.max { background: var(--max); } .src.gmail { background: var(--gmail); }

.search { position: sticky; top: -12px; z-index: 4; background: var(--bg); padding: 16px 0 8px; margin-top: -12px; }
.search input {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit;
}
.day {
  color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; margin: 14px 0 6px; padding: 4px 2px;
  position: sticky; top: 55px; z-index: 3; background: var(--bg);
}
.chat-day { align-self: center; top: 0; padding: 3px 10px; border-radius: 10px;
  background: color-mix(in srgb, var(--bg) 85%, var(--line)); margin: 10px 0 4px; }
details.stale { margin-top: 16px; }
details.stale > summary { color: var(--muted); font-size: 13px; font-weight: 600; cursor: pointer; padding: 8px 2px; list-style: none; }
details.stale > summary::before { content: '▸ '; }
details.stale[open] > summary::before { content: '▾ '; }
details.stale .card { opacity: .8; }
.row {
  display: grid; grid-template-columns: auto auto 1fr; gap: 8px; align-items: baseline;
  padding: 8px 2px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.row .text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .chatname { font-weight: 600; }
.more { display: block; margin: 16px auto; border: 1px solid var(--line); background: var(--surface); padding: 8px 18px; border-radius: 10px; cursor: pointer; }

.msgs { display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 85%; padding: 8px 12px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); align-self: flex-start; white-space: pre-wrap; word-wrap: break-word; }
.msg.out { align-self: flex-end; background: var(--mine); border-color: transparent; }
.msg.unread { background: var(--unread); }
.msg.deleted { opacity: .5; font-style: italic; }
.msg .meta { color: var(--muted); font-size: 12px; margin-bottom: 2px; }
.msg .att { color: var(--muted); font-size: 12px; margin-top: 2px; }

.sources .src-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; display: flex; gap: 10px; align-items: center; }
.sources .detail { color: var(--muted); font-size: 13px; }
.sources code { background: var(--line); padding: 1px 5px; border-radius: 5px; }

.login { max-width: 420px; margin: 40px auto; }
.login form { display: flex; gap: 8px; }
.login input { flex: 1; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text); font: inherit; }
.login button { border: 0; background: var(--accent); color: #fff; padding: 10px 16px; border-radius: 10px; cursor: pointer; }

.asks { margin-bottom: 14px; }
.asks-title { font-size: 12px; font-weight: 700; text-transform: uppercase; color: var(--muted); margin: 10px 2px 6px; }
.ask { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--line);
  border-left: 3px solid var(--warn); border-radius: 12px; padding: 8px 10px; margin-bottom: 6px; }
.ask-body { flex: 1; min-width: 0; cursor: pointer; }
.ask-what { font-weight: 500; }
.ask-meta { color: var(--muted); font-size: 12px; margin-top: 2px; display: flex; align-items: center; gap: 4px; }
.ask-close { border: 1px solid var(--line); background: none; border-radius: 50%; width: 32px; height: 32px; cursor: pointer; color: var(--ok); flex: none; }
.line.plot { font-weight: 500; }
.topic { color: var(--accent); font-weight: 600; }
.ask-inline { color: var(--warn); font-size: 13px; margin-top: 2px; }
.ask-inline.closed { color: var(--muted); text-decoration: line-through; }
.row.episode .text { font-weight: 500; }
.count { color: var(--muted); font-weight: 400; font-size: 12px; }
.ep-head { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; }
.ep-summary { font-weight: 600; margin: 2px 0; }
.ep-now { margin-top: 4px; }
.ep-meta { color: var(--muted); font-size: 12px; margin-top: 6px; }

.people-total { color: var(--muted); font-size: 12px; margin: 2px 2px 8px; }
.person { padding: 9px 2px; border-bottom: 1px solid var(--line); cursor: pointer; }
.person.bot { opacity: .6; }
.person-head { display: flex; align-items: center; gap: 6px; }
.person-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-meta { color: var(--muted); font-size: 13px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-keys { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 6px; }
.person-keys a { color: var(--accent); text-decoration: none; }
.members { margin-bottom: 8px; }
.member { display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 8px 10px; margin-bottom: 6px; }
.member-body { flex: 1; min-width: 0; }
.member-meta { color: var(--muted); font-size: 12px; margin-top: 2px; overflow-wrap: anywhere; }
.member-chat { border: 1px solid var(--line); background: none; border-radius: 10px; padding: 6px 12px; cursor: pointer; color: var(--accent); flex: none; }

.link { background: var(--surface); border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: 12px; padding: 10px; margin-bottom: 8px; }
.link.done { opacity: .4; pointer-events: none; }
.link-q { font-weight: 600; margin-bottom: 6px; }
.mini { padding: 6px 8px; border-radius: 10px; background: var(--bg); margin-bottom: 4px; cursor: pointer; }
.mini-head { display: flex; align-items: center; gap: 6px; }
.mini-name { font-weight: 500; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mini-meta { color: var(--muted); font-size: 12px; margin-top: 2px; overflow-wrap: anywhere; }
.link-why { color: var(--muted); font-size: 12px; margin: 4px 2px; }
.link-btns { display: flex; gap: 8px; margin-top: 6px; }
.link-btns button { flex: 1; border: 1px solid var(--line); background: none; border-radius: 10px; padding: 8px; cursor: pointer; font-weight: 500; }
.link-btns .yes { color: var(--ok); }
.link-btns .no { color: var(--err); }
.links-entry { display: block; width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface);
  border-left: 3px solid var(--accent); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; cursor: pointer; font-weight: 500; }
.merge-hint { background: var(--unread); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; font-size: 13px; }
.person-acts { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.person-acts button { border: 1px solid var(--line); background: none; border-radius: 10px; padding: 6px 12px; cursor: pointer; color: var(--accent); }
.member-chat.split { color: var(--muted); }

.theme-chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.theme-chips::-webkit-scrollbar { display: none; }
.theme-chips.inline { flex-wrap: wrap; overflow: visible; padding: 6px 0 0; }
.chip { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 14px; padding: 4px 10px;
  font-size: 13px; cursor: pointer; white-space: nowrap; color: var(--accent); }
.chip.all { color: var(--muted); }
.chip.person-chip { color: var(--text); }
.filter-bar { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.filter-name { flex: 1; min-width: 0; text-align: left; border: 1px solid var(--line); background: var(--surface);
  border-left: 3px solid var(--accent); border-radius: 10px; padding: 8px 10px; cursor: pointer; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filter-x { border: 1px solid var(--line); background: none; border-radius: 50%; width: 34px; height: 34px; cursor: pointer; color: var(--muted); flex: none; }
.row.theme-row { grid-template-columns: auto auto 1fr auto; }
.row-x { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0 4px; }

.status { border: 0; background: none; padding: 6px 2px; cursor: pointer; }
.theme-card .line { -webkit-line-clamp: 2; }
.theme-card.fresh, .theme-ep.fresh { border-left: 3px solid var(--accent); }
.theme-meta { color: var(--muted); font-size: 12px; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.theme-ep { padding: 10px 12px; }
.theme-ep .card-title { font-weight: 500; font-size: 14px; }
.theme-tag { color: var(--accent); font-size: 12px; }

.theme-label { border: 0; font: inherit; font-size: 11px; font-weight: 600; line-height: 18px; vertical-align: 1px;
  padding: 0 7px; margin-right: 5px; border-radius: 9px; cursor: pointer;
  color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }

.card-more { border: 0; background: none; color: var(--muted); font-size: 18px; line-height: 1; padding: 0 2px 4px 6px; cursor: pointer; }
.sheet-back { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: min(520px, 100%); background: var(--surface); border-radius: 16px 16px 0 0;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 6px; }
.sheet-title { color: var(--muted); font-size: 13px; text-align: center; padding: 4px 8px 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-btn { border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 12px; cursor: pointer; font-weight: 500; }
.sheet-btn.danger { color: var(--err); }
.sheet-btn.cancel { background: none; border: 0; color: var(--muted); }
.label-badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 9px; align-self: center; }
.label-badge.spam { color: var(--err); background: color-mix(in srgb, var(--err) 12%, transparent); }
.label-badge.low { color: var(--muted); background: color-mix(in srgb, var(--muted) 15%, transparent); }
.sifted-link { display: block; margin: 18px auto 4px; border: 0; background: none; color: var(--muted); font-size: 13px; cursor: pointer; }
.seg { display: flex; gap: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; margin-bottom: 8px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 9px; padding: 8px; cursor: pointer; color: var(--muted); font-weight: 500; }
.seg button.active { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.more.danger { color: var(--err); }
.row.dim { opacity: .55; }
.chat-bar { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin: -4px 0 6px; }
.chat-bar-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 2px 12px; cursor: pointer; color: var(--muted); font-size: 18px; line-height: 1.2; }

.msg-text { white-space: pre-wrap; }
.msg-text pre { white-space: pre-wrap; background: color-mix(in srgb, var(--text) 7%, transparent); padding: 6px 8px; border-radius: 8px; margin: 4px 0; font-size: 13px; overflow-x: auto; }
.msg-text code { background: color-mix(in srgb, var(--text) 8%, transparent); padding: 0 3px; border-radius: 4px; font-size: .92em; }
.msg-text blockquote { margin: 4px 0; padding: 2px 8px; border-left: 3px solid var(--accent); color: var(--muted); white-space: pre-wrap; }
.msg-text a { color: var(--accent); word-break: break-all; }
.msg-text .md-h { display: block; margin: 4px 0 2px; }
.spoiler { background: var(--muted); color: transparent; border-radius: 3px; cursor: pointer; }
.spoiler.shown { background: none; color: inherit; }
.mention { color: var(--accent); }
.fwd { color: var(--muted); font-size: 12px; font-style: italic; margin-bottom: 2px; }
.reply-quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin: 2px 0 4px; font-size: 13px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; background: color-mix(in srgb, var(--accent) 6%, transparent); border-radius: 0 6px 6px 0; }
.msg.flash { outline: 2px solid var(--accent); }
.atts { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.att-photo { max-width: min(320px, 100%); max-height: 360px; border-radius: 10px; cursor: zoom-in; object-fit: cover; background: var(--line); }
.att-sticker { width: 140px; height: auto; cursor: zoom-in; }
.att-video { position: relative; max-width: 320px; min-height: 60px; border-radius: 10px; overflow: hidden; background: #000; cursor: pointer; }
.att-video img { display: block; width: 100%; opacity: .85; }
.att-play { position: absolute; left: 8px; bottom: 8px; color: #fff; background: rgba(0,0,0,.55); border-radius: 12px; padding: 2px 10px; font-size: 13px; }
.att-audio { width: min(300px, 100%); height: 36px; }
.att-title { font-size: 12px; color: var(--muted); }
.att-file { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 10px; color: inherit; text-decoration: none; background: var(--bg); max-width: 320px; }
.att-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-size { color: var(--muted); font-size: 12px; flex: none; }
.att-link { border-left: 3px solid var(--accent); padding: 2px 8px; font-size: 13px; }
.att-link-title { font-weight: 600; }
.att-link-text { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.mail-open { margin-top: 6px; border: 1px solid var(--line); background: none; border-radius: 10px; padding: 4px 12px; cursor: pointer; color: var(--accent); }
.viewer { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.85); display: flex; align-items: center; justify-content: center; padding: 16px; }
.viewer-img, .viewer-video { max-width: 100%; max-height: 100%; border-radius: 6px; }
.viewer-x { position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 12px; border: 0; background: rgba(255,255,255,.15);
  color: #fff; width: 38px; height: 38px; border-radius: 50%; font-size: 18px; cursor: pointer; }
.mail-view { width: min(900px, 100%); height: 100%; display: flex; flex-direction: column; background: var(--surface); border-radius: 12px; overflow: hidden; }
.mail-bar { display: flex; gap: 8px; align-items: center; padding: 8px 52px 8px 12px; border-bottom: 1px solid var(--line); }
.mail-subj { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-bar button { border: 1px solid var(--line); background: none; border-radius: 10px; padding: 4px 10px; cursor: pointer; color: var(--accent); flex: none; }
.mail-frame { flex: 1; border: 0; width: 100%; background: #fff; }

.ask-reply { color: var(--accent); }
#dock { flex: none; position: relative; z-index: 5; background: var(--bg); border-top: 1px solid var(--line);
  padding: 8px max(16px, calc((100% - 760px) / 2)); }
.composer { margin: 0; }
.day-group { display: flex; flex-direction: column; gap: 6px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 9px 12px; max-height: 160px; }
.composer-send { border: 0; background: var(--accent); color: #fff; border-radius: 14px; padding: 10px 14px; cursor: pointer; font-weight: 600; flex: none; }
.composer-send:disabled { opacity: .5; }
.composer-reply { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin-bottom: 6px;
  border-left: 3px solid var(--accent); padding-left: 8px; }
.composer-reply span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer-off { font-size: 13px; color: var(--muted); }
.msg.pending { opacity: .75; border: 1px dashed var(--accent); }
.msg.failed { border-color: var(--err); }
.send-toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; margin-top: 6px; cursor: pointer; }

.send-toggle select { font: inherit; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); padding: 3px 6px; }

.asks { margin-top: 14px; }

/* ссылки между сущностями: имя → человек, название → чат, строка сюжета → сюжет */
.xlink { all: unset; cursor: pointer; color: inherit; font: inherit; border-bottom: 1px dotted color-mix(in srgb, currentColor 45%, transparent); }
.xlink:hover, .xlink:focus-visible { color: var(--accent); border-bottom-color: var(--accent); }
.xlink.card-title { border-bottom: 0; }
.xlink.card-title:hover { text-decoration: underline; }
.xlink-area { cursor: pointer; border-radius: 6px; }
.xlink-area:hover { background: color-mix(in srgb, var(--accent) 7%, transparent); }
.msg .meta .msg-open { margin-left: 6px; border-bottom: 0; color: var(--muted); }
.msg .meta .msg-open:hover { color: var(--accent); }
.chat-bar .open-in { border: 1px solid var(--line); background: var(--surface); border-radius: 10px; color: var(--accent); font-size: 13px; padding: 4px 10px; margin-right: auto; }
.ep-chat { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.ask.closed { opacity: .6; }
