:root {
  --bg: #f0f2f5; --panel: #fff; --text: #1c1e21; --muted: #65676b; --line: #e4e6eb;
  --primary: #1877f2; --primary-soft: #e7f0fd; --danger: #e41e3f; --ok: #31a24c; --warn: #f0a30a;
  --bubble-in: #f0f2f5; --bubble-out: #1877f2; --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.08), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18191a; --panel: #242526; --text: #e4e6eb; --muted: #b0b3b8; --line: #3a3b3c;
    --primary-soft: #263951; --bubble-in: #3a3b3c; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--primary); text-decoration: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1em; }
input, textarea, select {
  font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; width: 100%;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
label { display: grid; gap: 6px; font-weight: 600; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; cursor: pointer;
  font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 8px; background: var(--bg); color: var(--text);
  white-space: nowrap;
}
.btn:hover { filter: brightness(.96); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.link { background: none; padding: 2px 4px; color: var(--muted); font-weight: 600; }
.btn.link:hover { color: var(--primary); }
.btn.link.active { color: var(--primary); }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--panel); padding: 28px; border-radius: var(--radius); box-shadow: var(--shadow); width: min(380px, 100%); display: grid; gap: 16px; }

/* layout */
.app { display: grid; grid-template-columns: 220px 1fr; height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px; gap: 14px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; padding: 0 6px; }
.brand.big { font-size: 20px; }
.logo { width: 32px; height: 32px; border-radius: 8px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 800; }
nav { display: grid; gap: 2px; }
nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--text); font-weight: 600; }
nav a:hover { background: var(--bg); }
nav a.active { background: var(--primary-soft); color: var(--primary); }
nav a span { flex: 1; }
nav.apps { border-top: 1px solid var(--line); padding-top: 10px; }
nav.apps a { color: var(--muted); }
.badge { background: var(--danger); color: #fff; font-size: 11px; border-radius: 10px; padding: 1px 7px; }
.sidebar-foot { margin-top: auto; display: flex; justify-content: space-between; align-items: center; padding: 0 6px; }
.live { font-size: 12px; color: var(--muted); }
.live.on { color: var(--ok); }
main { overflow: auto; min-width: 0; }
.view { padding: 20px 24px; max-width: 1100px; }
.view-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.view-head h1 { font-size: 22px; margin: 0; flex: 1; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar select { width: auto; min-width: 180px; }
.toolbar label.check { display: flex; gap: 6px; align-items: center; font-weight: 500; }
.toolbar label.check input { width: auto; }
.card { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card h3 { margin: 0 0 8px; font-size: 16px; }
.notice { background: var(--primary-soft); border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; }
.notice.warn { background: rgba(240,163,10,.15); }
.notice.err { background: rgba(228,30,63,.1); color: var(--danger); max-height: 30vh; overflow-y: auto; flex-shrink: 0; }
.empty { text-align: center; color: var(--muted); padding: 40px 12px; }
code { background: var(--bg); padding: 1px 6px; border-radius: 4px; font-size: 12px; word-break: break-all; }

/* avatars */
.av { width: 36px; height: 36px; border-radius: 50%; flex: none; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 14px; object-fit: cover; }
.av.sm { width: 20px; height: 20px; font-size: 10px; }
.av.lg { width: 52px; height: 52px; font-size: 20px; }
.page-tag { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); font-weight: 600; max-width: 100%; }
.page-tag span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* pages */
.page-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.page-card { display: flex; gap: 12px; align-items: center; background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }
.page-card.off { opacity: .55; }
.page-card .info { flex: 1; min-width: 0; }
.page-card .name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.switch { position: relative; width: 38px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 22px; cursor: pointer; transition: .2s; }
.switch span::before { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::before { transform: translateX(16px); }
.token-form { display: flex; gap: 8px; }

/* inbox */
.inbox { display: grid; grid-template-columns: 340px 1fr; height: 100%; }
.conv-col { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.conv-col .toolbar { padding: 12px; border-bottom: 1px solid var(--line); }
.conv-col .toolbar select { flex: 1; min-width: 0; }
.conv-list { overflow: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--primary-soft); }
.conv .body { flex: 1; min-width: 0; }
.conv .top { display: flex; justify-content: space-between; gap: 6px; }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .name, .conv.unread .snippet { font-weight: 800; color: var(--text); }
.conv .snippet { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--primary); display: inline-block; }
.thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--panel); }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.thread-head .back { display: none; }
.messages { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: 70%; display: flex; flex-direction: column; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { background: var(--bubble-in); padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; max-width: 100%; }
.msg.out .bubble { background: var(--bubble-out); color: #fff; }
.msg img.att, .msg video.att { max-width: 260px; max-height: 300px; border-radius: 12px; margin-top: 2px; }
.msg .time { font-size: 11px; color: var(--muted); margin: 2px 6px 6px; }
.composer { border-top: 1px solid var(--line); padding: 10px 12px; display: grid; gap: 6px; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 40px; max-height: 160px; }
.composer .opts { display: flex; gap: 12px; align-items: center; font-size: 12px; color: var(--muted); }
.composer .opts label { display: flex; align-items: center; gap: 4px; font-weight: 500; }
.composer .opts input { width: auto; }

/* comments */
.comments-view { max-width: 1100px; margin: 0 auto; }
.comments-head { align-items: flex-start; gap: 16px; }
.comments-head > div:first-child { flex: 1 0 200px; }
.comments-subtitle { margin: 4px 0 0; }
.comments-head .toolbar { width: 100%; padding: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; gap: 10px 16px; }
.comments-head .toolbar select { flex: 1; min-width: 200px; max-width: 380px; }
.comments-head .toolbar label.check { font-size: 13px; cursor: pointer; }
.comments-head input[type=checkbox] { accent-color: var(--primary); width: 16px; height: 16px; }
.comments-head #reloadComments { margin-left: auto; }
#commentSync { margin: 0 2px 12px; }
#commentErrors .notice { font-size: 12px; padding: 10px 12px; border-left: 3px solid var(--danger); overflow-wrap: anywhere; }
.comment { display: grid; gap: 12px; padding: 14px; border: 1px solid var(--line); box-shadow: 0 1px 2px rgba(0,0,0,.03); margin-bottom: 10px; }
.comment .post-ref { display: flex; gap: 10px; align-items: center; background: var(--bg); border-radius: 8px; padding: 8px 10px; color: var(--muted); font-size: 12px; min-width: 0; }
.comment .post-ref:hover { background: var(--primary-soft); }
.comment .post-ref > img { width: 40px; height: 40px; flex: none; object-fit: cover; border-radius: 6px; }
.post-ref-body { flex: 1; min-width: 0; }
.comment .page-tag { display: flex; margin-bottom: 2px; }
.comment .page-tag .av.sm { width: 18px; height: 18px; border-radius: 50%; }
.post-excerpt { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow-wrap: anywhere; }
.post-ref-open { flex: none; font-size: 18px; }
.c-row { display: flex; gap: 10px; min-width: 0; align-items: flex-start; }
.c-body { flex: 1; min-width: 0; }
.c-bubble { background: var(--bg); border-radius: 4px 14px 14px; padding: 8px 12px; display: inline-block; max-width: 100%; overflow-wrap: anywhere; }
.c-bubble b { display: block; font-size: 13px; line-height: 1.5; }
.c-message { white-space: pre-wrap; margin-top: 2px; line-height: 1.5; }
.c-bubble.hidden-c { opacity: .5; }
.c-att { max-width: min(220px, 100%); max-height: 260px; object-fit: contain; border-radius: 10px; margin-top: 6px; display: block; }
.c-actions { display: flex; gap: 2px 6px; flex-wrap: wrap; align-items: center; margin-top: 4px; font-size: 12px; }
.c-actions > .muted { margin-right: 4px; font-size: 11px; }
.c-actions .btn.link { font-size: 12px; padding: 5px 6px; }
.c-actions .btn.link:hover { background: var(--primary-soft); filter: none; }
.c-actions .c-reply { color: var(--primary); }
.c-actions .btn.danger { color: var(--danger); }
.c-actions .btn:focus-visible, .post-ref:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.replies { margin-left: 46px; display: grid; gap: 12px; border-left: 2px solid var(--line); padding-left: 12px; }
.replies:empty { display: none; }
.reply-box { display: flex; gap: 8px; margin-left: 46px; align-items: flex-start; }
.reply-box textarea { min-height: 38px; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 10px; background: var(--bg); color: var(--muted); }
.tag.page { background: var(--primary-soft); color: var(--primary); }
@media (max-width: 700px) {
  .comments-head .toolbar { gap: 10px; }
  .comments-head .toolbar select { flex-basis: 100%; max-width: none; min-width: 0; }
  .comments-head #reloadComments { margin-left: 0; }
  .comment { padding: 12px; }
  .replies { margin-left: 18px; padding-left: 10px; }
  .reply-box { margin-left: 0; flex-wrap: wrap; }
  .reply-box textarea { flex-basis: 100%; }
}

/* compose */
.compose { display: grid; grid-template-columns: 1fr 320px; gap: 16px; align-items: start; }
.compose .card { display: grid; gap: 14px; }
.compose textarea[name=message] { min-height: 160px; }
.drop { border: 2px dashed var(--line); border-radius: 10px; padding: 18px; text-align: center; color: var(--muted); cursor: pointer; }
.drop.over { border-color: var(--primary); background: var(--primary-soft); }
.previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
.preview { position: relative; aspect-ratio: 1; border-radius: 8px; overflow: hidden; background: var(--bg); }
.preview img, .preview video { width: 100%; height: 100%; object-fit: cover; }
.preview button { position: absolute; top: 4px; right: 4px; border: 0; border-radius: 50%; width: 22px; height: 22px; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; }
.preview .size { position: absolute; bottom: 4px; left: 4px; font-size: 11px; background: rgba(0,0,0,.6); color: #fff; padding: 0 5px; border-radius: 4px; }
.page-pick { display: grid; gap: 2px; max-height: 420px; overflow: auto; }
.page-pick label { display: flex; align-items: center; gap: 8px; font-weight: 500; padding: 6px; border-radius: 8px; cursor: pointer; }
.page-pick label:hover { background: var(--bg); }
.page-pick input { width: auto; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline input[type=checkbox] { width: auto; }
.progress { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--primary); transition: width .3s; }

/* history */
.post-head { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.post-head .msg-preview { flex: 1; min-width: 200px; white-space: pre-wrap; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.targets { display: grid; gap: 6px; margin-top: 12px; }
.target { display: grid; grid-template-columns: minmax(140px, 220px) 110px 1fr; gap: 10px; align-items: center; font-size: 13px; }
.target .err { color: var(--danger); font-size: 12px; }
.status { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; text-align: center; }
.status.done { background: rgba(49,162,76,.15); color: var(--ok); }
.status.error { background: rgba(228,30,63,.12); color: var(--danger); }
.status.partial { background: rgba(240,163,10,.18); color: var(--warn); }
.status.running, .status.pending { background: var(--primary-soft); color: var(--primary); }

/* toasts */
#toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; max-width: min(360px, calc(100vw - 32px)); }
.toast { background: var(--panel); color: var(--text); box-shadow: 0 4px 16px rgba(0,0,0,.18); border-radius: 10px; padding: 10px 14px; border-left: 4px solid var(--primary); cursor: pointer; animation: slide .2s ease-out; }
.toast.err { border-color: var(--danger); }
.toast.ok { border-color: var(--ok); }
@keyframes slide { from { transform: translateY(10px); opacity: 0; } }

/* responsive */
@media (max-width: 900px) {
  .compose { grid-template-columns: 1fr; }
  .inbox { grid-template-columns: 1fr; }
  .inbox.has-thread .conv-col { display: none; }
  .inbox:not(.has-thread) .thread { display: none; }
  .thread-head .back { display: inline-flex; }
}
@media (max-width: 700px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .sidebar { order: 2; flex-direction: row; padding: 4px; border-right: 0; border-top: 1px solid var(--line); }
  .sidebar .brand, .sidebar-foot, nav.apps { display: none; }
  nav { grid-auto-flow: column; width: 100%; }
  nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 2px; text-align: center; position: relative; }
  nav a .badge { position: absolute; top: 2px; right: 10%; }
  .view { padding: 16px; }
  .target { grid-template-columns: 1fr auto; }
  .target > :last-child { grid-column: 1 / -1; }
  .msg { max-width: 85%; }
}

/* page access */
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0; }
.chip { font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 10px; background: var(--bg); color: var(--muted); }
details.card summary { cursor: pointer; }
details.card[open] summary { margin-bottom: 10px; }
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal-card { background: var(--panel); border-radius: var(--radius); padding: 20px; width: min(440px, 100%); max-height: 90vh; overflow: auto; box-shadow: 0 8px 30px rgba(0,0,0,.25); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; }

.health { font-weight: 600; margin: 2px 0; }
.health.ok { color: #1a8f4c; }
.health.bad { color: var(--danger); }
.health.warn { color: #b26a00; }
.status.cancelled { background: rgba(120,120,120,.15); color: var(--muted); }
.stats { color: var(--text, inherit); }
.stats-total { margin-top: 8px; padding: 6px 10px; background: var(--primary-soft); border-radius: 8px; }
