:root {
  --red: #C9222C; --red-dark: #a51b23; --ink: #1f2937; --muted: #6b7280; --line: #e5e7eb;
  --bg: #f3f4f6; --card: #fff; --in: #fff; --out: #dcf8c6; --note: #fff7d6;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg); }
button, select, input, textarea { font: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 6px 12px; }
button:disabled { opacity: .5; cursor: default; }

.app { display: grid; grid-template-rows: auto 1fr auto; height: 100dvh; }
.top { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--red); color: #fff; }
.brand { font-size: 17px; flex: 1; }
.me { font-size: 13px; opacity: .9; }
.icon { background: transparent; border: 0; color: inherit; font-size: 22px; padding: 0 6px; }
.foot { text-align: center; font-size: 11px; color: var(--muted); padding: 4px; }

.main { display: grid; grid-template-columns: 340px 1fr; min-height: 0; }
.list { border-right: 1px solid var(--line); background: var(--card); display: flex; flex-direction: column; min-height: 0; }
.filters { padding: 10px; border-bottom: 1px solid var(--line); }
.filters input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; }
.tabs { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.tabs button { padding: 4px 10px; font-size: 13px; border-radius: 999px; }
.tabs button.on { background: var(--red); color: #fff; border-color: var(--red); }
.convs { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.convs li { padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.convs li:hover, .convs li.on { background: #fef2f2; }
.convs .n { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convs .t { font-size: 12px; color: var(--muted); }
.convs .p { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convs .b { font-size: 11px; color: var(--muted); text-align: right; }
.convs .u { background: var(--red); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 7px; margin-left: 6px; }
.pill { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: #eef2ff; color: #3730a3; }
.pill.queued { background: #fee2e2; color: #991b1b; } .pill.assigned { background: #dbeafe; color: #1e40af; }
.pill.waiting_customer { background: #e5e7eb; color: #374151; } .pill.closed { background: #f3f4f6; color: #6b7280; }

.thread { display: flex; min-height: 0; background: #ece5dd; }
.empty { margin: auto; color: var(--muted); }
.conv { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.conv-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 14px; background: var(--card); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.who { font-weight: 600; } .sub { font-size: 12px; color: var(--muted); }
.actions { display: flex; gap: 8px; } .actions select { padding: 6px; border-radius: 8px; border: 1px solid var(--line); max-width: 180px; }
.banner { background: #fff3cd; color: #7a5b00; padding: 8px 14px; font-size: 13px; }
.msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.m { max-width: min(560px, 82%); padding: 7px 10px 5px; border-radius: 8px; background: var(--in); box-shadow: 0 1px 1px rgba(0,0,0,.08); white-space: pre-wrap; word-wrap: break-word; position: relative; }
.m.out { align-self: flex-end; background: var(--out); }
.m.note { align-self: center; background: var(--note); font-size: 13px; max-width: 90%; }
.m.sys { align-self: center; background: transparent; box-shadow: none; color: var(--muted); font-size: 12px; }
.m .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.m .by { font-size: 12px; font-weight: 600; color: var(--red-dark); }
.m .err { color: #b91c1c; font-size: 12px; }
.m img, .m video { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; }
.m audio { max-width: 260px; }
.m .rep { font-size: 11px; color: var(--muted); cursor: pointer; }
.composer { background: var(--card); border-top: 1px solid var(--line); padding: 8px 10px; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer .small { font-size: 12px; color: var(--muted); margin-top: 4px; }
.composer textarea { flex: 1; resize: none; max-height: 160px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; }
.composer button[type=submit] { background: var(--red); color: #fff; border-color: var(--red); padding: 8px 16px; }
.composer .attach { color: var(--muted); cursor: pointer; padding-bottom: 6px; }
.reply-to { font-size: 12px; background: #f3f4f6; border-left: 3px solid var(--red); padding: 4px 8px; margin-bottom: 6px; display: flex; justify-content: space-between; }

.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); }
.login .card { background: #fff; padding: 28px 32px; border-radius: 12px; text-align: center; box-shadow: 0 2px 10px rgba(0,0,0,.06); }
.login a { display: inline-block; margin-top: 12px; background: var(--red); color: #fff; padding: 10px 18px; border-radius: 8px; text-decoration: none; }
.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); background: #111827; color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 99999; font-size: 14px; }

.only-mobile { display: none; }
@media (max-width: 760px) {
  .main { grid-template-columns: 1fr; }
  .only-mobile { display: inline; }
  .app.in-thread .list { display: none; }
  .app:not(.in-thread) .thread { display: none; }
  .actions select { max-width: 130px; }
}
