:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sunken: #f0f2f5;
  --line: #e3e6eb;
  --line-strong: #cfd4dc;
  --text: #1b1f27;
  --muted: #5f6877;
  --faint: #8b93a1;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --accent-soft: #e8effd;
  --note: #fff6d6;
  --note-line: #f0dc8c;
  --good: #1f9d55;
  --warn: #c27c0e;
  --bad: #d23b3b;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --panel: #181b21;
    --sunken: #1f232a;
    --line: #2a2f38;
    --line-strong: #3a404b;
    --text: #e8eaee;
    --muted: #a3abb8;
    --faint: #7c8594;
    --accent-soft: #1c2a47;
    --note: #3a3218;
    --note-line: #5c4e1d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 var(--font); color: var(--text); background: var(--bg); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12px; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px;
  border: 1px solid var(--line-strong); border-radius: 8px; background: var(--panel); cursor: pointer;
  font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--sunken); }
.btn:focus-visible, .input:focus-visible, textarea:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--sunken); }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .55; cursor: default; }
.input, select.input, textarea.input {
  width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--panel);
}
textarea.input { height: auto; padding: 8px 10px; resize: vertical; }
label.field { display: grid; gap: 4px; margin-bottom: 12px; font-weight: 500; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.error { color: var(--bad); min-height: 20px; }

/* ---------- login ---------- */
.login { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login form { width: 100%; max-width: 360px; background: var(--panel); padding: 28px; border-radius: 14px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login p { margin: 0 0 20px; color: var(--muted); }
.login .btn { width: 100%; justify-content: center; height: 38px; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 220px 340px 1fr; height: 100vh; }
.shell.with-details { grid-template-columns: 220px 340px 1fr 280px; }
.shell.settings-mode { grid-template-columns: 220px 1fr; }

.sidebar { background: var(--sunken); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px; gap: 2px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; font-weight: 650; }
.brand .logo { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; flex: none; }
.brand .logo svg { width: 16px; height: 16px; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 8px; color: var(--text); text-decoration: none; }
.nav-item:hover { background: var(--line); }
.nav-item.active { background: var(--panel); box-shadow: var(--shadow); font-weight: 600; }
.nav-item .count { margin-left: auto; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.nav-item .count.hot { color: var(--accent); font-weight: 650; }
.nav-sep { height: 1px; background: var(--line); margin: 10px 8px; }
.sidebar .spacer { flex: 1; }
.me { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; }
.me .who { min-width: 0; flex: 1; }
.me .who div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.conn.live { background: var(--good); }

.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; flex: none; font-size: 12px; font-weight: 650; color: #fff; position: relative; }
.avatar.sm { width: 22px; height: 22px; font-size: 10px; }
.avatar .dot { position: absolute; right: -1px; bottom: -1px; width: 9px; height: 9px; border-radius: 50%; background: var(--good); border: 2px solid var(--panel); }

/* ---------- list ---------- */
.list { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-width: 0; }
.list-head { display: flex; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); gap: 8px; }
.list-head h2 { font-size: 16px; margin: 0; flex: 1; }
.list-body { overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 30px 1fr; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; text-decoration: none; color: inherit; }
.conv:hover { background: var(--sunken); }
.conv.active { background: var(--accent-soft); }
.conv .top { display: flex; gap: 8px; align-items: baseline; }
.conv .name { font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .time { font-size: 12px; color: var(--faint); flex: none; }
.conv .preview { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 2px; }
.conv.unread .name::after { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-left: 6px; vertical-align: middle; }
.conv.unread .preview { color: var(--text); }
.conv .meta { display: flex; gap: 6px; margin-top: 6px; align-items: center; }
.empty { padding: 48px 24px; text-align: center; color: var(--muted); }
.empty strong { display: block; color: var(--text); margin-bottom: 4px; font-size: 15px; }

.pill { display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 7px; border-radius: 999px; font-size: 11.5px; font-weight: 600; background: var(--sunken); color: var(--muted); }
.pill.open { background: #e5f6ec; color: #17784a; }
.pill.pending { background: #fdf1dc; color: #9a620a; }
.pill.closed { background: var(--sunken); color: var(--muted); }
@media (prefers-color-scheme: dark) {
  .pill.open { background: #163626; color: #6ad49b; }
  .pill.pending { background: #3a2c12; color: #e8b25b; }
}

/* ---------- conversation ---------- */
.thread { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); min-height: 57px; flex-wrap: wrap; }
.thread-head .title { flex: 1; min-width: 160px; }
.thread-head .title div:first-child { font-weight: 650; font-size: 15px; }
.thread-head select { width: auto; height: 32px; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 20px 24px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: min(560px, 80%); display: flex; flex-direction: column; gap: 3px; }
.msg .bubble { padding: 9px 13px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg .by { font-size: 12px; color: var(--faint); padding: 0 4px; }
.msg.contact { align-self: flex-start; }
.msg.contact .bubble { background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.agent { align-self: flex-end; }
.msg.agent .by { text-align: right; }
.msg.agent .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.msg.note { align-self: flex-end; }
.msg.note .by { text-align: right; }
.msg.note .bubble { background: var(--note); border: 1px dashed var(--note-line); color: var(--text); border-bottom-right-radius: 4px; }
.msg.system { align-self: center; max-width: 100%; }
.msg.system .bubble { background: none; padding: 2px 8px; color: var(--faint); font-size: 12px; text-align: center; }
.day { align-self: center; font-size: 12px; color: var(--faint); margin: 6px 0; }

.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 10px 16px 14px; }
.composer .tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.composer .tab { border: 0; background: none; padding: 4px 10px; border-radius: 6px; cursor: pointer; color: var(--muted); font-weight: 600; }
.composer .tab.on { background: var(--sunken); color: var(--text); }
.composer .tab.on.note { background: var(--note); }
.composer textarea { width: 100%; min-height: 64px; max-height: 240px; border: 1px solid var(--line-strong); border-radius: 10px; padding: 10px 12px; resize: none; background: var(--panel); }
.composer.note-mode textarea { background: var(--note); border-color: var(--note-line); }
.composer .row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.composer .row .hint { flex: 1; font-size: 12px; color: var(--faint); }

/* ---------- details ---------- */
.details { border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto; padding: 18px; min-width: 0; }
.details h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--faint); margin: 20px 0 8px; }
.details h3:first-child { margin-top: 0; }
.kv { display: grid; grid-template-columns: 72px 1fr; gap: 6px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.history a { display: block; padding: 7px 8px; border-radius: 6px; text-decoration: none; color: var(--text); font-size: 13px; }
.history a:hover { background: var(--sunken); }
.history a.current { background: var(--accent-soft); }

.placeholder { display: grid; place-items: center; height: 100%; color: var(--muted); text-align: center; padding: 24px; }

/* ---------- settings ---------- */
.settings { overflow-y: auto; padding: 28px 32px 64px; }
.settings h1 { font-size: 22px; margin: 0 0 4px; }
.settings > p { margin: 0 0 24px; color: var(--muted); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; margin-bottom: 20px; max-width: 760px; }
.card h2 { font-size: 16px; margin: 0 0 4px; }
.card > p { margin: 0 0 16px; color: var(--muted); }
table.team { width: 100%; border-collapse: collapse; }
table.team td, table.team th { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.team th { font-size: 12px; color: var(--faint); font-weight: 600; }
table.team td.actions { text-align: right; white-space: nowrap; }
table.team tr.disabled td { opacity: .55; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
pre.snippet { background: var(--sunken); border: 1px solid var(--line); border-radius: 8px; padding: 12px; font: 12.5px/1.5 var(--mono); overflow-x: auto; white-space: pre-wrap; word-break: break-all; margin: 0 0 10px; }
.swatch { display: flex; gap: 8px; align-items: center; }
.swatch input[type=color] { width: 40px; height: 36px; padding: 2px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--panel); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 9px 16px; border-radius: 8px; box-shadow: var(--shadow); z-index: 10; font-weight: 500; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .shell.with-details { grid-template-columns: 220px 300px 1fr; }
  .shell.with-details .details { display: none; }
}
@media (max-width: 860px) {
  .shell, .shell.with-details, .shell.settings-mode { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .shell.show-sidebar .sidebar { display: flex; }
  .shell.show-sidebar .list, .shell.show-sidebar .thread, .shell.show-sidebar .settings { display: none; }
  .shell.has-conv .list { display: none; }
  .shell:not(.has-conv) .thread { display: none; }
  .back { display: inline-flex; }
  .menu-btn { display: inline-flex !important; }
  .settings { padding: 20px 16px 48px; }
  .grid2 { grid-template-columns: 1fr; }
}
.menu-btn { display: none !important; }
