:root {
  --bg: #0a0e17;
  --panel: #0f1522;
  --panel-2: #141b2b;
  --panel-3: #1a2336;
  --line: rgba(255, 255, 255, 0.07);
  --line-2: rgba(255, 255, 255, 0.12);
  --text: #e6ebf5;
  --muted: #8a94a8;
  --faint: #5d6679;
  --accent: #22d3ee;
  --accent-ink: #03222a;
  --accent-soft: rgba(34, 211, 238, 0.12);
  --out: #0d3440;
  --in: #172034;
  --amber: #f5b83d;
  --amber-soft: rgba(245, 184, 61, 0.14);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.12);
  --ok: #34d399;
  --ok-soft: rgba(52, 211, 153, 0.13);
  --violet: #a78bfa;
  --radius: 12px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.5; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }

/* ---------- boot ---------- */
#app.boot { display: grid; place-items: center; height: 100%; }
.boot-mark { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line-2); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--line-2); background: var(--panel-2);
  font-weight: 600; font-size: 13px; white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover:not(:disabled) { background: var(--panel-3); border-color: rgba(255,255,255,0.2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { background: #5ee3f5; }
.btn.danger { color: var(--danger); }
.btn.small { height: 28px; padding: 0 10px; font-size: 12px; border-radius: 8px; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--panel-2); }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 0; background: transparent;
  display: inline-grid; place-items: center; color: var(--muted); flex-shrink: 0;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.active { color: var(--accent); background: var(--accent-soft); }
.icon-btn svg, .btn svg { width: 18px; height: 18px; }

.input, .select, .textarea {
  width: 100%; height: 38px; padding: 0 12px;
  background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 10px;
  outline: none; transition: border-color 0.15s;
}
.textarea { height: auto; padding: 10px 12px; resize: vertical; min-height: 80px; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); }
.select { appearance: none; padding-right: 30px; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 16px) 16px, calc(100% - 11px) 16px; background-size: 5px 5px; background-repeat: no-repeat; }
.select option { background: var(--panel); }
label.field { display: grid; gap: 6px; font-size: 12px; font-weight: 600; color: var(--muted); }

.switch { position: relative; width: 38px; height: 22px; flex-shrink: 0; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--panel-3); border: 1px solid var(--line-2); transition: background 0.15s; pointer-events: none; }
.switch span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: transform 0.15s, background 0.15s; }
.switch input:checked + span { background: var(--accent-soft); border-color: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); background: var(--accent); }

.chip {
  display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 8px;
  border-radius: 99px; font-size: 11px; font-weight: 600; white-space: nowrap;
  background: var(--panel-3); color: var(--muted);
}
.stage-new { background: var(--accent-soft); color: var(--accent); }
.stage-negotiating { background: rgba(167, 139, 250, 0.14); color: var(--violet); }
.stage-payment_claimed { background: var(--amber-soft); color: var(--amber); }
.stage-payment_received { background: var(--ok-soft); color: var(--ok); }
.stage-delivered { background: var(--ok-soft); color: var(--ok); }
.stage-closed { background: var(--panel-3); color: var(--faint); }
.chip.warn { background: var(--danger-soft); color: var(--danger); }
.chip.bot { background: var(--panel-3); color: var(--muted); }

/* ---------- login ---------- */
.login { display: grid; place-items: center; height: 100%; padding: 24px; background: radial-gradient(800px 400px at 50% -10%, rgba(34, 211, 238, 0.10), transparent); }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); display: grid; place-items: center; color: var(--accent-ink); }
.brand-mark svg { width: 18px; height: 18px; }
.login h1 { font-size: 22px; margin: 8px 0 0; letter-spacing: -0.01em; }
.login p { margin: 0; }
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 60px 360px 1fr; height: 100vh; height: 100dvh; }
.shell.with-details { grid-template-columns: 60px 360px 1fr 320px; }
.shell.page-mode { grid-template-columns: 60px 1fr; }

.rail { background: var(--bg); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 0; }
.rail .brand-mark { margin-bottom: 10px; }
.rail .spacer { flex: 1; }
.rail .icon-btn { width: 42px; height: 42px; position: relative; }
.rail .dot { position: absolute; top: 7px; right: 7px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 99px; background: var(--accent); color: var(--accent-ink); font-size: 10px; font-weight: 700; display: grid; place-items: center; }
.avatar-me { width: 34px; height: 34px; border-radius: 50%; background: var(--panel-3); display: grid; place-items: center; font-weight: 700; font-size: 12px; border: 0; }

/* ---------- chat list ---------- */
.list { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.list-head { padding: 16px 16px 10px; display: grid; gap: 10px; border-bottom: 1px solid var(--line); }
.list-head h2 { margin: 0; font-size: 18px; letter-spacing: -0.01em; }
.search { position: relative; }
.search svg { position: absolute; left: 11px; top: 10px; width: 16px; height: 16px; color: var(--faint); }
.search .input { padding-left: 34px; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; margin: 0 -2px; padding: 0 2px; }
.tabs::-webkit-scrollbar { display: none; }
.tab { height: 28px; padding: 0 10px; border-radius: 8px; border: 0; background: transparent; color: var(--muted); font-size: 12px; font-weight: 600; white-space: nowrap; display: inline-flex; align-items: center; gap: 5px; }
.tab:hover { background: var(--panel-2); color: var(--text); }
.tab.active { background: var(--panel-3); color: var(--text); }
.tab .n { font-size: 10px; padding: 1px 5px; border-radius: 99px; background: var(--panel-2); color: var(--muted); }
.tab.active .n { background: var(--accent); color: var(--accent-ink); }
.list-filter { display: flex; gap: 8px; align-items: center; }
.list-filter .select { height: 32px; font-size: 12px; }

.chats { flex: 1; overflow-y: auto; min-height: 0; }
.chat-row { display: grid; grid-template-columns: 42px 1fr auto; gap: 10px; align-items: center; padding: 11px 16px; border: 0; background: transparent; width: 100%; text-align: left; border-bottom: 1px solid var(--line); }
.chat-row:hover { background: var(--panel-2); }
.chat-row.active { background: var(--panel-3); box-shadow: inset 3px 0 0 var(--accent); }
.avatar { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 14px; color: #0a0e17; flex-shrink: 0; }
.chat-main { min-width: 0; display: grid; gap: 3px; }
.chat-top { display: flex; align-items: center; gap: 6px; min-width: 0; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-ref { color: var(--faint); font-size: 11px; white-space: nowrap; }
.chat-preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row.unread .chat-preview { color: var(--text); }
.chat-meta { display: grid; justify-items: end; gap: 5px; align-self: start; padding-top: 2px; }
.chat-time { font-size: 11px; color: var(--faint); white-space: nowrap; }
.chat-row.unread .chat-time { color: var(--accent); }
.badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 99px; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700; display: grid; place-items: center; }
.chat-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 2px; }
.empty { padding: 48px 24px; text-align: center; color: var(--muted); display: grid; gap: 6px; justify-items: center; }
.empty strong { color: var(--text); font-size: 15px; }

/* ---------- conversation ---------- */
.convo { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.convo-empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }
.convo-empty .big { width: 56px; height: 56px; border-radius: 16px; background: var(--panel-2); display: grid; place-items: center; margin: 0 auto 14px; color: var(--accent); }
.convo-empty .big svg { width: 26px; height: 26px; }
.convo-head { height: 64px; padding: 0 12px 0 16px; display: flex; align-items: center; gap: 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-shrink: 0; }
.convo-head .who { min-width: 0; flex: 1; display: grid; }
.convo-head .who b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.convo-head .sub { font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; white-space: nowrap; overflow: hidden; }
.phone { color: var(--amber); font-variant-numeric: tabular-nums; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 16px 6% 8px; min-height: 0; display: flex; flex-direction: column; gap: 2px;
  background-image: radial-gradient(rgba(255,255,255,0.025) 1px, transparent 1px); background-size: 18px 18px; }
.load-more { align-self: center; margin-bottom: 8px; }
.day { align-self: center; margin: 12px 0 8px; padding: 3px 10px; border-radius: 8px; background: var(--panel-2); color: var(--muted); font-size: 11px; font-weight: 600; }
.msg { max-width: min(75%, 560px); display: flex; flex-direction: column; margin-top: 6px; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg + .msg.same { margin-top: 2px; }
.who-tag { font-size: 11px; font-weight: 600; color: var(--muted); margin: 0 6px 3px; display: flex; gap: 5px; align-items: center; }
.who-tag.bot { color: var(--violet); }
.who-tag.phone-tag { color: var(--amber); }
.bubble { padding: 7px 10px 6px; border-radius: 12px; background: var(--in); word-wrap: break-word; overflow-wrap: anywhere; white-space: pre-wrap; position: relative; }
.msg.out .bubble { background: var(--out); }
.msg.out.bot .bubble { background: #221d3a; }
.msg.failed .bubble { box-shadow: inset 0 0 0 1px var(--danger); }
.bubble .foot { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 10.5px; color: var(--muted); margin-top: 3px; white-space: nowrap; }
.bubble .foot svg { width: 14px; height: 14px; }
.bubble .foot .sent { color: var(--accent); }
.bubble .media { display: block; max-width: 100%; max-height: 340px; border-radius: 8px; margin: -1px -4px 5px; cursor: zoom-in; background: var(--panel-2); }
.bubble video.media, .bubble audio { cursor: auto; }
.bubble audio { width: 260px; max-width: 100%; margin-bottom: 4px; }
.bubble .sticker { max-width: 140px; background: transparent; }
.bubble .doc { display: flex; gap: 8px; align-items: center; padding: 8px; border-radius: 8px; background: rgba(0,0,0,0.2); color: var(--text); text-decoration: none; margin-bottom: 4px; }
.bubble .placeholder { font-size: 12px; color: var(--muted); font-style: italic; }
.bubble pre { margin: 6px 0 0; padding: 8px; border-radius: 8px; background: rgba(0,0,0,0.3); font-size: 11px; white-space: pre-wrap; color: var(--amber); }
.fail-row { display: flex; gap: 6px; align-items: center; font-size: 11px; color: var(--danger); margin: 3px 4px 0; }

.bot-bar { display: flex; align-items: center; gap: 10px; padding: 8px 16px; font-size: 12.5px; border-top: 1px solid var(--line); background: var(--panel); flex-shrink: 0; }
.bot-bar .grow { flex: 1; min-width: 0; }
.bot-bar.live { color: var(--violet); }
.bot-bar.human { color: var(--muted); }

.composer { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); display: flex; gap: 8px; align-items: flex-end; flex-shrink: 0; position: relative; }
.composer textarea { flex: 1; resize: none; max-height: 160px; min-height: 40px; height: 40px; padding: 10px 12px; border-radius: 12px; background: var(--panel-2); border: 1px solid var(--line-2); outline: none; line-height: 1.35; }
.composer textarea:focus { border-color: var(--accent); }
.send { width: 40px; height: 40px; border-radius: 12px; border: 0; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; flex-shrink: 0; }
.send svg { width: 18px; height: 18px; }
.attach-preview { position: absolute; bottom: 100%; left: 12px; right: 12px; margin-bottom: 8px; padding: 8px; border-radius: 12px; background: var(--panel-3); border: 1px solid var(--line-2); display: flex; gap: 10px; align-items: center; box-shadow: 0 10px 30px rgba(0,0,0,0.4); }
.attach-preview img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; }
.popover { position: absolute; bottom: 100%; left: 12px; width: min(380px, calc(100% - 24px)); max-height: 320px; overflow-y: auto; margin-bottom: 8px; border-radius: 12px; background: var(--panel-3); border: 1px solid var(--line-2); box-shadow: 0 16px 40px rgba(0,0,0,0.45); padding: 6px; z-index: 5; }
.popover button { display: grid; gap: 2px; width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 8px; }
.popover button:hover, .popover button.hl { background: var(--panel-2); }
.popover b { font-size: 13px; }
.popover span { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- details ---------- */
.details { background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; min-height: 0; display: flex; flex-direction: column; }
.details-head { height: 64px; display: flex; align-items: center; justify-content: space-between; padding: 0 12px 0 18px; border-bottom: 1px solid var(--line); flex-shrink: 0; }
.details-head b { font-size: 15px; }
.section { padding: 16px 18px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.section h4 { margin: 0; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--faint); }
.row { display: flex; align-items: center; gap: 10px; justify-content: space-between; }
.row .txt { display: grid; gap: 2px; min-width: 0; }
.row .txt small { color: var(--muted); font-size: 12px; }
.note { padding: 10px; border-radius: 10px; background: var(--amber-soft); display: grid; gap: 4px; }
.note p { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.note small { color: var(--muted); font-size: 11px; }
.id-card { display: flex; gap: 12px; align-items: center; }
.id-card .avatar { width: 48px; height: 48px; font-size: 16px; }

/* ---------- pages ---------- */
.page { overflow-y: auto; min-height: 0; background: var(--bg); }
.page-inner { max-width: 920px; margin: 0 auto; padding: 28px 24px 60px; display: grid; gap: 22px; }
.page h1 { margin: 0; font-size: 22px; letter-spacing: -0.01em; }
.page-head { display: flex; justify-content: space-between; align-items: end; gap: 16px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; display: grid; gap: 14px; }
.card h3 { margin: 0; font-size: 15px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.member { display: grid; grid-template-columns: 40px 1fr auto; gap: 12px; align-items: center; }
.member .avatar { width: 40px; height: 40px; }
.perm-list { display: grid; gap: 10px; padding-top: 12px; border-top: 1px solid var(--line); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--faint); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; }
td.nowrap { white-space: nowrap; }
.link { color: var(--accent); background: none; border: 0; padding: 0; font-weight: 600; }

/* ---------- overlays ---------- */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,0.92); display: grid; place-items: center; z-index: 50; padding: 20px; cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--panel-3); border: 1px solid var(--line-2); padding: 10px 16px; border-radius: 10px; z-index: 60; box-shadow: 0 10px 30px rgba(0,0,0,0.5); font-weight: 600; max-width: calc(100% - 32px); }
.toast.err { border-color: var(--danger); color: var(--danger); }
.offline { position: fixed; top: 10px; left: 50%; transform: translateX(-50%); background: var(--danger-soft); color: var(--danger); border: 1px solid var(--danger); padding: 5px 12px; border-radius: 99px; font-size: 12px; font-weight: 600; z-index: 40; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .shell.with-details { grid-template-columns: 60px 320px 1fr; }
  .shell.with-details .details { position: fixed; right: 0; top: 0; bottom: 0; width: 340px; z-index: 30; box-shadow: -20px 0 40px rgba(0,0,0,0.5); }
}
@media (max-width: 820px) {
  .shell, .shell.with-details, .shell.page-mode { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .rail { flex-direction: row; justify-content: space-around; border-right: 0; border-top: 1px solid var(--line); padding: 4px 8px calc(4px + env(safe-area-inset-bottom)); order: 2; }
  .rail .brand-mark, .rail .spacer { display: none; }
  .list, .convo, .page { grid-row: 1; grid-column: 1; }
  .shell.chat-open .list { display: none; }
  .shell:not(.chat-open) .convo { display: none; }
  .shell.chat-open .rail { display: none; }
  .shell.chat-open { grid-template-rows: 1fr; }
  .back { display: inline-grid; }
  .messages { padding: 12px 10px 6px; }
  .msg { max-width: 86%; }
  .shell.with-details .details { width: 100%; }
  .grid-2 { grid-template-columns: 1fr; }
  .page-inner { padding: 20px 16px 40px; }
  .convo-head { padding: 0 8px; gap: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
