:root {
  --bg: #f4f6f8; --panel: #fff; --line: #e3e7ec; --text: #17212b; --muted: #6b7785;
  --brand: #0b5c6b; --brand-2: #0e7a8d; --in: #fff; --out: #dcf3ef; --note: #fff6d6; --danger: #c0392b;
  --radius: 10px; font-size: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
.spacer { flex: 1; }
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-2); outline-offset: -1px; border-color: transparent; }
label { display: block; color: var(--muted); font-size: 12px; margin: 10px 0 4px; }
label input, label select { margin-top: 4px; color: var(--text); font-size: 14px; }

.btn { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 7px 12px; }
.btn:hover { background: #f0f3f6; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.primary:disabled { opacity: .5; cursor: default; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.error { color: var(--danger); min-height: 1em; font-size: 13px; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; }
.logo { background: var(--brand); color: #fff; border-radius: 7px; padding: 3px 6px; font-size: 12px; letter-spacing: .5px; }

/* login */
.login { height: 100%; display: grid; place-items: center; }
.login-card { background: var(--panel); padding: 28px; border-radius: 14px; width: 340px; box-shadow: 0 6px 30px rgba(0,0,0,.06); }
.login-card .brand { margin-bottom: 12px; font-size: 18px; }
.login-card .btn { width: 100%; margin-top: 16px; padding: 10px; }

/* app shell */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.me { color: var(--muted); padding: 0 6px; }
.layout { flex: 1; display: grid; grid-template-columns: 330px 1fr 270px; min-height: 0; }

/* sidebar */
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.views, .statuses { display: flex; gap: 4px; padding: 8px 10px 0; }
.views button, .statuses button, .composer-tabs button, .status-btns button {
  border: 0; background: transparent; padding: 6px 10px; border-radius: 7px; color: var(--muted);
}
.views button.active, .statuses button.active, .composer-tabs button.active, .status-btns button.active { background: #e6f1f3; color: var(--brand); font-weight: 600; }
.views b, .statuses b { font-weight: 600; font-size: 11px; background: #eef1f4; border-radius: 9px; padding: 1px 6px; margin-left: 2px; }
.views b:empty, .statuses b:empty { display: none; }
.statuses button { font-size: 12px; padding: 4px 8px; }
.search { margin: 8px 10px; width: auto; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv-list li { padding: 10px 12px; border-bottom: 1px solid #f0f2f5; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.conv-list li:hover { background: #f7f9fa; }
.conv-list li.active { background: #e6f1f3; }
.conv-list .n { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-list .t { color: var(--muted); font-size: 11px; text-align: right; }
.conv-list .p { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-list .u { background: #1fa855; color: #fff; border-radius: 10px; font-size: 11px; padding: 0 7px; justify-self: end; align-self: center; }
.conv-list .meta { grid-column: 1 / -1; display: flex; gap: 6px; font-size: 11px; color: var(--muted); }
.conv-list li.unread .n, .conv-list li.unread .p { color: var(--text); }
.tag { border-radius: 5px; padding: 0 5px; background: #eef1f4; }
.tag.pending { background: #fff1d6; color: #8a5a00; }
.tag.closed { background: #eceff1; }
.tag.expired { background: #fdecea; color: var(--danger); }
.empty-list { padding: 30px; text-align: center; color: var(--muted); }

/* thread */
.thread { display: flex; flex-direction: column; min-height: 0; background: #eef1ef; }
.thread .empty { margin: auto; color: var(--muted); }
.thread-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head { display: flex; align-items: center; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.thread-head .title { font-weight: 650; font-size: 15px; }
.thread-head .sub { color: var(--muted); font-size: 12px; }
.pill { font-size: 12px; border-radius: 12px; padding: 3px 10px; }
.pill.open { background: #e2f6ea; color: #1b7a43; }
.pill.closed { background: #fdecea; color: var(--danger); }
.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; background: #fff; color: var(--muted); font-size: 11px; padding: 3px 10px; border-radius: 8px; margin: 8px 0; }
.msg { max-width: 68%; padding: 7px 10px 5px; border-radius: 9px; background: var(--in); box-shadow: 0 1px 1px rgba(0,0,0,.05); overflow-wrap: anywhere; position: relative; }
.msg.out { align-self: flex-end; background: var(--out); }
.msg.note { align-self: center; background: var(--note); max-width: 80%; border: 1px dashed #e6c766; }
.msg.system { align-self: center; background: transparent; box-shadow: none; color: var(--muted); font-size: 12px; padding: 2px; }
.msg.external { opacity: .75; font-style: italic; }
.msg .who { font-size: 11px; color: var(--brand); font-weight: 600; margin-bottom: 2px; }
.msg .foot { font-size: 10.5px; color: var(--muted); text-align: right; margin-top: 2px; display: flex; gap: 6px; justify-content: flex-end; align-items: center; }
.msg .tick.read { color: #1d9bf0; }
.msg .err { color: var(--danger); font-size: 12px; margin-top: 4px; white-space: normal; }
.msg img, .msg video { max-width: 100%; max-height: 320px; border-radius: 6px; display: block; margin-bottom: 4px; }
.msg audio { max-width: 260px; }
.msg .doc { display: inline-flex; gap: 6px; align-items: center; padding: 8px; background: rgba(0,0,0,.04); border-radius: 6px; text-decoration: none; color: var(--text); margin-bottom: 4px; }
.msg .quote { border-left: 3px solid var(--brand-2); background: rgba(0,0,0,.04); padding: 3px 7px; border-radius: 4px; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg .replybtn { position: absolute; top: 2px; right: -26px; border: 0; background: transparent; color: var(--muted); opacity: 0; }
.msg.out .replybtn { right: auto; left: -26px; }
.msg:hover .replybtn { opacity: 1; }
.msg a { color: var(--brand-2); }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px 10px; }
.composer-tabs { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; }
.composer.note-mode textarea { background: var(--note); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 160px; }
.warn { margin-top: 6px; color: #8a5a00; background: #fff6e0; border-radius: 6px; padding: 6px 10px; font-size: 12.5px; }
.replying { font-size: 12px; background: #eef4f5; border-left: 3px solid var(--brand-2); padding: 4px 8px; margin-bottom: 6px; display: flex; justify-content: space-between; }

/* details */
.details { background: var(--panel); border-left: 1px solid var(--line); padding: 4px 16px; overflow-y: auto; }
.details h3 { margin: 12px 0 4px; font-size: 14px; }
.details .btn.small { margin-top: 6px; }
.status-btns { display: flex; gap: 4px; }
.status-btns button { border: 1px solid var(--line); }
.hint { color: var(--muted); font-size: 12px; line-height: 1.4; }

/* modals */
.modal { position: fixed; inset: 0; background: rgba(15,25,35,.35); display: grid; place-items: center; z-index: 10; }
.modal-card { background: #fff; border-radius: 14px; padding: 20px 22px; width: 460px; max-height: 88vh; overflow-y: auto; }
.modal-card.wide { width: 760px; }
.modal-card h3 { margin: 0 0 6px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.tpl-preview { white-space: pre-wrap; background: #f4f7f8; border-radius: 8px; padding: 10px; margin-top: 10px; font-size: 13px; }
.tpl-preview:empty { display: none; }
table.agents { width: 100%; border-collapse: collapse; font-size: 13px; }
table.agents th, table.agents td { text-align: left; padding: 6px; border-bottom: 1px solid var(--line); }
.add-agent { display: grid; grid-template-columns: 1fr 1.3fr 1.3fr .8fr auto; gap: 6px; }

@media (max-width: 1100px) { .layout { grid-template-columns: 290px 1fr; } .details { display: none !important; } }
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .app.in-thread .sidebar { display: none; }
  .app:not(.in-thread) .thread { display: none; }
  .messages { padding: 12px; }
}
.thread-head .back { display: none; }
@media (max-width: 720px) { .thread-head .back { display: inline-block; } }
.msg .txt { white-space: pre-wrap; }
