:root {
  --bg: #121110; --panel: #1a1917; --panel-2: #222120; --line: #33302c;
  --cream: #F2E8D5; --cream-dim: #bfb5a3; --muted: #8f8778;
  --red: #C0392B; --red-hi: #D9483A; --red-dim: #5a1f1a;
  --in-bubble: #2a2825; --out-bubble: #F2E8D5; --out-text: #1a1411;
  --radius: 14px; --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--cream); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--cream-dim); }
.muted { color: var(--muted); } .small { font-size: 12.5px; }
.error { color: #ff8f84; margin: 4px 0; }
[hidden] { display: none !important; }

.btn { border: 1px solid transparent; border-radius: 10px; padding: 8px 14px; cursor: pointer; background: var(--panel-2); min-height: 40px; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--red); color: #fff; font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: var(--red-hi); }
.btn-outline { border-color: var(--cream-dim); background: transparent; color: var(--cream); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.btn-danger { background: transparent; border-color: #a33; color: #ff8f84; }
input, select, textarea { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-height: 40px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--red); outline-offset: 0; border-color: transparent; }
.badge { font-size: 11.5px; font-weight: 700; letter-spacing: .04em; padding: 3px 8px; border-radius: 999px; background: var(--red-dim); color: #ffd9d4; text-transform: uppercase; }
.badge.live { background: #1f3b26; color: #bdf0c8; }

/* Login */
.login-body { display: grid; place-items: center; min-height: 100%; padding: 20px; background: radial-gradient(circle at 50% 0%, #2a1512 0%, var(--bg) 60%); }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px 24px; text-align: center; border-top: 4px solid var(--red); }
.login-logo { width: 150px; height: 150px; }
.login-card h1 { margin: 10px 0 2px; font-size: 22px; letter-spacing: .03em; }
.login-card form { text-align: left; margin-top: 18px; display: grid; gap: 12px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--cream-dim); }

/* App shell */
.app { display: flex; flex-direction: column; height: 100dvh; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 14px; background: #0c0b0a; border-bottom: 2px solid var(--red); }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 50%; flex: none; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { letter-spacing: .04em; text-transform: uppercase; font-size: 14.5px; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.conn { color: #666; font-size: 12px; } .conn.on { color: #58c46b; } .conn.off { color: var(--red); }
.menu { position: relative; }
.menu-pop { position: absolute; right: 0; top: calc(100% + 6px); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; min-width: 200px; z-index: 30; padding: 6px; box-shadow: 0 10px 30px #000a; }
.menu-pop button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.menu-pop button:hover { background: var(--panel-2); }
.demo-banner { background: repeating-linear-gradient(45deg, #3a1714, #3a1714 12px, #44201b 12px, #44201b 24px); color: #ffd9d4; text-align: center; font-size: 13px; font-weight: 600; padding: 6px 10px; }

.panes { flex: 1; display: grid; grid-template-columns: minmax(290px, 360px) 1fr; min-height: 0; }
.list-pane { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.list-tools { padding: 10px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.list-tools input[type=search] { width: 100%; }
.filters { display: flex; gap: 6px; }
.filters select { flex: 1; min-width: 0; }
.seg { display: flex; background: var(--panel-2); border-radius: 10px; padding: 3px; border: 1px solid var(--line); }
.seg button { border: 0; background: none; padding: 6px 10px; border-radius: 8px; cursor: pointer; color: var(--cream-dim); white-space: nowrap; }
.seg button.active { background: var(--cream); color: var(--out-text); font-weight: 600; }
.thread-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.thread-item { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 11px 12px 11px 14px; border-bottom: 1px solid #2a2724; cursor: pointer; position: relative; }
.thread-item:hover { background: #211f1d; }
.thread-item.active { background: #2b1916; box-shadow: inset 3px 0 0 var(--red); }
.thread-item .t-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item.unread .t-name { color: #fff; }
.thread-item .t-time { font-size: 12px; color: var(--muted); text-align: right; }
.thread-item .t-preview { color: var(--cream-dim); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item.unread .t-preview { color: var(--cream); font-weight: 500; }
.thread-item .t-meta { display: flex; gap: 6px; align-items: center; justify-content: flex-end; }
.unread-badge { background: var(--red); color: #fff; border-radius: 999px; font-size: 11.5px; font-weight: 700; min-width: 20px; height: 20px; display: inline-grid; place-items: center; padding: 0 6px; }
.chip { font-size: 11px; padding: 1px 7px; border-radius: 999px; border: 1px solid var(--line); color: var(--cream-dim); white-space: nowrap; max-width: 130px; overflow: hidden; text-overflow: ellipsis; }
.chip.closed { color: var(--muted); }
.chip.waiting { border-color: var(--red); color: #ffb3aa; }
.t-sub { grid-column: 1 / -1; display: flex; gap: 6px; margin-top: 3px; }
.empty { padding: 20px; text-align: center; }

.thread-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.no-thread { flex: 1; display: grid; place-content: center; text-align: center; opacity: .8; }
.no-thread img { margin: 0 auto; opacity: .35; }
.thread-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.back-btn { display: none; font-size: 26px; line-height: 1; padding: 4px 10px; }
.who { flex: 1; min-width: 150px; display: flex; flex-direction: column; }
.contact-name { background: transparent; border: 1px solid transparent; font-weight: 700; font-size: 17px; padding: 2px 6px; min-height: 32px; margin-left: -6px; }
.contact-name:hover { border-color: var(--line); }
.thread-actions { display: flex; gap: 6px; align-items: center; }
.thread-actions select { max-width: 190px; }
.messages { flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 6px; background: linear-gradient(#141312, #121110); }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.msg { max-width: min(78%, 560px); display: flex; flex-direction: column; }
.msg .bubble { padding: 9px 13px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in { align-self: flex-start; } .msg.in .bubble { background: var(--in-bubble); border-bottom-left-radius: 6px; }
.msg.out { align-self: flex-end; align-items: flex-end; } .msg.out .bubble { background: var(--out-bubble); color: var(--out-text); border-bottom-right-radius: 6px; }
.msg.failed .bubble { outline: 2px solid var(--red); }
.msg .meta { font-size: 11.5px; color: var(--muted); margin: 3px 6px 0; }
.msg .meta .who-sent { color: var(--cream-dim); font-weight: 600; }
.msg .meta .failed { color: #ff8f84; font-weight: 600; }
.msg img.media { max-width: 240px; border-radius: 12px; margin-top: 4px; display: block; }
.reply { border-top: 1px solid var(--line); padding: 10px 12px calc(10px + var(--safe-b)); background: var(--panel); display: grid; gap: 6px; }
.reply textarea { width: 100%; resize: none; max-height: 40vh; min-height: 44px; line-height: 1.35; }
.reply-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

.sim { position: fixed; right: 16px; bottom: 16px; width: 290px; background: var(--panel); border: 1px dashed var(--red); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; z-index: 20; box-shadow: 0 10px 30px #000a; }
.sim h3 { margin: 0; font-size: 14px; }
.sim p { margin: 0; }

.modal { background: var(--panel); color: var(--cream); border: 1px solid var(--line); border-radius: 16px; padding: 0; width: min(520px, 94vw); }
.modal::backdrop { background: #000b; }
.modal .m-inner { padding: 18px; display: grid; gap: 12px; }
.modal h2 { margin: 0; font-size: 18px; }
.modal label { display: grid; gap: 5px; font-size: 13px; color: var(--cream-dim); }
.modal .row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.staff-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.staff-table td { padding: 8px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.staff-table td:last-child { text-align: right; white-space: nowrap; }
.staff-table .btn { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
pre.status { background: var(--bg); padding: 10px; border-radius: 10px; overflow: auto; max-height: 50vh; font-size: 12px; }
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%); background: var(--cream); color: var(--out-text); padding: 10px 16px; border-radius: 12px; font-weight: 600; z-index: 50; max-width: 90vw; box-shadow: 0 8px 24px #000a; }
.toast.err { background: var(--red); color: #fff; }

/* Phones & Z Fold cover screen: single pane */
@media (max-width: 760px) {
  .panes { grid-template-columns: 1fr; }
  .panes .thread-pane { display: none; }
  .panes.show-thread .list-pane { display: none; }
  .panes.show-thread .thread-pane { display: flex; }
  .back-btn { display: inline-block; }
  .brand-text strong { font-size: 13px; }
  .thread-actions { width: 100%; }
  .thread-actions select { flex: 1; max-width: none; }
  .msg { max-width: 88%; }
  .sim { left: 10px; right: 10px; width: auto; bottom: 10px; }
  .grid2 { grid-template-columns: 1fr; }
}
@media (max-width: 380px) {
  .brand-text span { display: none; }
  .topbar { padding: 6px 8px; }
  .seg button { padding: 6px 7px; }
}
.brand-text strong { overflow: hidden; text-overflow: ellipsis; }
#user-btn { white-space: nowrap; }
@media (max-width: 420px) {
  .brand-text strong { font-size: 12px; letter-spacing: .02em; }
  #user-name { display: inline-block; max-width: 64px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
  #user-btn { padding: 6px 6px; }
  .topbar-right { gap: 4px; }
  .demo-banner { font-size: 11.5px; }
}
.brand { flex: 1 1 auto; overflow: hidden; }
.topbar-right { flex: none; }
