:root {
  --bg: #f4f5f7; --panel: #ffffff; --line: #e3e5e9; --text: #1d2129; --muted: #6b7280;
  --accent: #2563eb; --accent-soft: #e8efff; --in: #ffffff; --out: #dcf3e4; --note: #fff6d6;
  --danger: #dc2626; --ok: #16a34a; --warn: #d97706; --side: #1f2937;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #111418; --panel: #1a1e24; --line: #2b313a; --text: #e6e8eb; --muted: #9aa3ae;
    --accent: #5b8cff; --accent-soft: #1f2a44; --in: #222831; --out: #17352a; --note: #3a3218; --side: #0b0d10; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 12px; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.danger { color: var(--danger); }
button.link { border: 0; background: none; padding: 2px 4px; color: var(--accent); }
input, select, textarea { border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; background: var(--panel); width: 100%; }
textarea { resize: vertical; }
label { display: block; margin: 8px 0; color: var(--muted); font-size: 13px; }
label input, label select, label textarea { margin-top: 4px; color: var(--text); }
label.chk { display: flex; align-items: center; gap: 6px; }
label.chk input { width: auto; margin: 0; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; margin-top: 8px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; }

.login { display: grid; place-items: center; height: 100%; padding: 16px; }
.login .card { width: 100%; max-width: 360px; }
.login h1 { margin: 0; }
.login button { width: 100%; margin-top: 12px; padding: 10px; }

.app { display: flex; height: 100%; }
.side { width: 60px; background: var(--side); display: flex; flex-direction: column; align-items: center; padding: 10px 0; gap: 6px; }
.side .brand { color: #fff; font-weight: 700; margin-bottom: 10px; }
.side .nav { width: 42px; height: 42px; border: 0; background: transparent; font-size: 19px; border-radius: 10px; padding: 0; }
.side .nav.active, .side .nav:hover { background: rgba(255,255,255,.12); }
.side .nav { position: relative; }
.side .nav .badge { position: absolute; top: -2px; right: -2px; padding: 0 5px; font-size: 11px; }
.settings { max-width: 440px; margin-top: 16px; }
.grow { flex: 1; }
.view { flex: 1; min-width: 0; }

.chats { display: flex; height: 100%; }
.list { width: 340px; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; }
.filters { padding: 10px; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.filters .row { display: flex; gap: 6px; }
.filters label.chk { margin: 0; }
.convs { overflow-y: auto; flex: 1; }
.conv { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); }
.conv .top { display: flex; justify-content: space-between; gap: 8px; }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .meta { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.conv .top .meta { flex: none; flex-wrap: nowrap; }
.conv .tag { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; }
.tag { border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; font-size: 11px; color: var(--muted); white-space: nowrap; }
.st-new { color: var(--ok); } .st-open { color: var(--accent); } .st-pending { color: var(--warn); } .st-closed { color: var(--muted); }
.group { position: sticky; top: 0; z-index: 1; width: 100%; display: flex; align-items: center; gap: 6px; padding: 7px 12px;
  background: var(--bg); color: var(--text); border: 0; border-bottom: 1px solid var(--line); border-radius: 0;
  font-size: 12px; font-weight: 600; text-align: left; cursor: pointer; }
.group .arrow { width: 10px; color: var(--muted); }
.group .count { color: var(--muted); font-weight: 400; flex: 1; }

.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.chat .empty { margin: auto; color: var(--muted); }
.chat-head { padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.chat-head .who { flex: 1; min-width: 160px; }
.chat-head .who b { font-size: 15px; }
.chat-head select, .chat-head input { width: auto; }
.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 72%; padding: 8px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--in); align-self: flex-start; position: relative; word-wrap: break-word; }
.msg.out { background: var(--out); align-self: flex-end; }
.msg.note { background: var(--note); align-self: center; max-width: 85%; border-style: dashed; }
.msg .txt { white-space: pre-wrap; }
.msg .foot { font-size: 11px; color: var(--muted); display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 2px; }
.msg .actions { position: absolute; top: -12px; right: 8px; display: none; gap: 2px; }
.msg:hover .actions { display: flex; }
.msg.in .actions { right: auto; left: 8px; }
.msg .actions:focus-within { display: flex; }
.msg .actions button { padding: 0 6px; font-size: 12px; }
.msg .actions select { width: auto; padding: 0 4px; font-size: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); cursor: pointer; }
.msg.flash { outline: 2px solid var(--accent); }
.quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin-bottom: 4px; font-size: 12px; color: var(--muted); cursor: pointer; white-space: pre-wrap; overflow: hidden; max-height: 4.5em; }
.atts { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.atts img { max-width: 220px; max-height: 180px; border-radius: 8px; cursor: zoom-in; display: block; }
.file { display: inline-flex; gap: 6px; align-items: center; border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; background: var(--panel); font-size: 13px; text-decoration: none; color: var(--text); }
.failed { color: var(--danger); }

.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 10px 12px; }
.composer .replybox, .composer .pending { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.composer .replybox .quote { flex: 1; margin: 0; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 200px; }
.composer.note textarea { background: var(--note); }
.composer .tools { display: flex; gap: 6px; flex-direction: column; }
.dropzone { position: absolute; inset: 0; background: rgba(37,99,235,.12); border: 3px dashed var(--accent); display: grid; place-items: center; font-size: 18px; z-index: 5; pointer-events: none; }

.page { padding: 20px 24px; overflow-y: auto; }
.page header { display: flex; justify-content: space-between; align-items: center; }
.page h2 { margin: 0; }
.tbl { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-top: 12px; }
.tbl th, .tbl td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.tbl th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--bg); }
.tbl td.actions { white-space: nowrap; text-align: right; }
.code { font-family: ui-monospace, Menlo, monospace; font-size: 12px; word-break: break-all; }

dialog { border: 0; padding: 0; background: transparent; max-width: 560px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog .card { max-height: 88vh; overflow-y: auto; }
dialog h3 { margin-top: 0; }
dialog .btns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.perms { display: grid; gap: 2px; }
.hint { font-size: 12px; color: var(--muted); background: var(--bg); border-radius: 8px; padding: 8px 10px; margin: 8px 0; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--side); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: 90%; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 40; cursor: zoom-out; }
.lightbox img { max-width: 95vw; max-height: 95vh; }

@media (max-width: 800px) {
  .side { width: 48px; } .side .nav { width: 38px; height: 38px; }
  .list { width: 100%; }
  .chats.open .list { display: none; }
  .chats:not(.open) .chat { display: none; }
  .msg { max-width: 90%; }
  .page { padding: 16px; }
  .tbl { display: block; overflow-x: auto; }
}
.mobile-back { display: none; }
@media (max-width: 800px) { .mobile-back { display: inline-block; } }
