:root {
  --bg: #f4f5f2;
  --panel: #ffffff;
  --panel-2: #f9faf8;
  --line: #e3e5df;
  --text: #1c1f1d;
  --muted: #6b716c;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e0f0ec;
  --danger: #b42318;
  --warn: #b54708;
  --ok: #15803d;
  --bubble-in: #ffffff;
  --bubble-out: #dcefe9;
  --chat-bg: #eceee9;
  --shadow: 0 1px 2px rgba(20, 24, 22, .06), 0 4px 16px rgba(20, 24, 22, .06);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111413; --panel: #181c1a; --panel-2: #1d221f; --line: #2a302d; --text: #e6e9e6; --muted: #929a95;
    --accent: #2bb3a3; --accent-ink: #062521; --danger: #f97066; --warn: #fdb022; --ok: #47cd89; --accent-soft: #173430; --bubble-in: #1f2522; --bubble-out: #16403a;
    --chat-bg: #131715; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111413; --panel: #181c1a; --panel-2: #1d221f; --line: #2a302d; --text: #e6e9e6; --muted: #929a95;
  --accent: #2bb3a3; --accent-ink: #062521; --danger: #f97066; --warn: #fdb022; --ok: #47cd89; --accent-soft: #173430; --bubble-in: #1f2522; --bubble-out: #16403a;
  --chat-bg: #131715; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel); cursor: pointer; white-space: nowrap; font-weight: 500;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 13px; }
.btn.icon { width: 34px; padding: 0; }
.btn:disabled { opacity: .5; cursor: default; }
input.field, select.field, textarea.field {
  width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
textarea.field { height: auto; padding: 8px 10px; resize: vertical; }
.field:focus, .btn:focus-visible { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); outline-offset: 1px; }
label.lbl { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth form { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.auth h1 { font-size: 20px; margin: 0 0 4px; }
.auth p { margin: 0 0 12px; color: var(--muted); }
.auth .btn { width: 100%; margin-top: 18px; height: 40px; }
.err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 10px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 64px 1fr; height: 100dvh; }
.rail { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; align-items: center; padding: 12px 0; gap: 4px; }
.rail .logo { width: 36px; height: 36px; border-radius: 10px; background: var(--accent); display: grid; place-items: center; margin-bottom: 12px; }
.rail button { position: relative; width: 44px; height: 44px; border-radius: 10px; border: 0; background: transparent; cursor: pointer; display: grid; place-items: center; color: var(--muted); }
.rail button:hover { background: var(--panel-2); color: var(--text); }
.rail button.on { background: var(--accent-soft); color: var(--accent); }
.rail .spacer { flex: 1; }
.rail .dot { position: absolute; top: 7px; right: 7px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--danger); color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; }
svg.i { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.main { min-width: 0; min-height: 0; }

/* ---------- inbox ---------- */
.inbox { display: grid; grid-template-columns: 340px 1fr auto; height: 100%; min-height: 0; }
.list { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 12px; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.list-head .row { display: flex; gap: 6px; }
.tabs { display: flex; gap: 2px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.tabs button { flex: 1; border: 0; background: transparent; border-radius: 6px; height: 28px; cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 500; }
.tabs button.on { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
.tabs .n { font-size: 11px; color: var(--muted); margin-left: 3px; }
.tabs .n.hot { color: var(--danger); font-weight: 700; }
.chats { overflow-y: auto; flex: 1; }
.chat-item { display: grid; grid-template-columns: 40px 1fr auto; gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); align-items: center; }
.chat-item:hover { background: var(--panel-2); }
.chat-item.on { background: var(--accent-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 600; color: #fff; font-size: 15px; flex: none; }
.chat-item .top { display: flex; gap: 6px; align-items: baseline; min-width: 0; }
.chat-item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .prev { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .meta { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 3px; }
.chat-item .side { display: grid; justify-items: end; gap: 4px; font-size: 11px; color: var(--muted); }
.badge { background: var(--accent); color: var(--accent-ink); border-radius: 10px; min-width: 20px; height: 20px; padding: 0 6px; font-size: 11px; font-weight: 700; display: grid; place-items: center; }
.pill { display: inline-flex; align-items: center; gap: 4px; height: 18px; padding: 0 7px; border-radius: 9px; font-size: 11px; font-weight: 500; background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.pill.tag { color: #fff; border: 0; }
.pill.tag button { border: 0; background: none; color: inherit; cursor: pointer; padding: 0; font-size: 12px; line-height: 1; }
.empty { padding: 32px 20px; text-align: center; color: var(--muted); }

.conv { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); }
.conv-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 58px; flex-wrap: wrap; }
.conv-head .who { flex: 1; min-width: 140px; }
.conv-head .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-head .who span { color: var(--muted); font-size: 12px; }
.conv-head select.field { width: auto; height: 32px; max-width: 180px; }
.back { display: none; }
.msgs { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; background: var(--panel); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 8px; margin: 10px 0 6px; box-shadow: var(--shadow); }
.msg { max-width: min(75%, 560px); padding: 6px 9px 4px; border-radius: 9px; background: var(--bubble-in); box-shadow: var(--shadow); align-self: flex-start; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.me { align-self: flex-end; background: var(--bubble-out); }
.msg .who { font-size: 11px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.msg .txt { white-space: pre-wrap; }
.msg .foot { display: flex; justify-content: flex-end; gap: 4px; font-size: 11px; color: var(--muted); margin-top: 2px; align-items: center; }
.msg .ack.read { color: #34b7f1; }
.msg img.media, .msg video.media { display: block; max-width: 100%; max-height: 340px; border-radius: 6px; margin-bottom: 4px; cursor: pointer; background: var(--panel-2); min-height: 60px; min-width: 120px; }
.msg img.sticker { max-width: 140px; background: none; box-shadow: none; }
.msg audio { width: 260px; max-width: 100%; }
.msg .doc { display: flex; gap: 8px; align-items: center; padding: 8px; background: var(--panel-2); border-radius: 6px; margin-bottom: 4px; text-decoration: none; color: var(--text); }
.msg .unsup { font-style: italic; color: var(--muted); }
.more { align-self: center; margin-bottom: 8px; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 10px; position: relative; }
.composer .row { display: flex; gap: 6px; align-items: flex-end; }
.composer textarea { flex: 1; min-height: 38px; max-height: 160px; resize: none; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); }
.composer .opts { display: flex; align-items: center; gap: 12px; font-size: 12px; color: var(--muted); padding: 0 2px 6px; flex-wrap: wrap; }
.composer .opts label { display: inline-flex; gap: 5px; align-items: center; cursor: pointer; }
.attach { display: flex; align-items: center; gap: 8px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; margin-bottom: 6px; font-size: 13px; }
.attach span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qr-pop { position: absolute; bottom: 100%; left: 10px; right: 10px; max-height: 260px; overflow-y: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); }
.qr-pop div { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.qr-pop div.on, .qr-pop div:hover { background: var(--accent-soft); }
.qr-pop b { color: var(--accent); margin-right: 8px; }
.qr-pop small { color: var(--muted); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.offline { background: color-mix(in srgb, var(--warn) 12%, var(--panel)); color: var(--warn); padding: 6px 12px; font-size: 13px; }

.details { width: 300px; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; }
.details h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 8px; }
.details h3:first-child { margin-top: 0; }
.details .tags { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 8px; }
.note { background: color-mix(in srgb, #eab308 10%, var(--panel)); border: 1px solid color-mix(in srgb, #eab308 30%, var(--line)); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; }
.note .txt { white-space: pre-wrap; }
.note .by { font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; margin-top: 4px; }
.note .by button { border: 0; background: none; color: var(--danger); cursor: pointer; font-size: 11px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 13px; }
.kv span:nth-child(odd) { color: var(--muted); }
.placeholder { display: grid; place-items: center; height: 100%; color: var(--muted); text-align: center; padding: 20px; }

/* ---------- pages ---------- */
.page { padding: 24px; overflow-y: auto; height: 100%; }
.page-inner { max-width: 960px; margin: 0 auto; }
.page h1 { font-size: 22px; margin: 0 0 4px; }
.page .sub { color: var(--muted); margin: 0 0 20px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.conn-card { display: grid; gap: 10px; align-content: start; }
.conn-card .top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.conn-card .top b { font-size: 15px; }
.conn-card img.qr { width: 100%; max-width: 260px; justify-self: center; background: #fff; border-radius: 8px; padding: 6px; }
.conn-card .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.st { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.st::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.st.connected { color: var(--ok); }
.st.qr, .st.connecting { color: var(--warn); }
.st.disconnected { color: var(--danger); }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 16px; }
.form-row > * { flex: 1; min-width: 160px; }
.form-row > .btn { flex: none; }
table.t { width: 100%; border-collapse: collapse; }
table.t th, table.t td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.t th { font-size: 12px; color: var(--muted); font-weight: 500; }
.table-wrap { overflow-x: auto; }
.qr-list .item { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); align-items: flex-start; }
.qr-list .item b { color: var(--accent); min-width: 90px; }
.qr-list .item p { margin: 0; flex: 1; white-space: pre-wrap; }

/* ---------- modal & toast ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--panel); border-radius: 14px; padding: 20px; width: 100%; max-width: 400px; box-shadow: var(--shadow); }
.modal h2 { margin: 0 0 6px; font-size: 17px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 10px 16px; border-radius: 10px; font-size: 13px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; max-width: calc(100% - 32px); }
#toast.on { opacity: 1; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 70; cursor: zoom-out; }
.lightbox img { max-width: 95vw; max-height: 95vh; }

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .details { position: fixed; right: 0; top: 0; bottom: 0; z-index: 40; box-shadow: -8px 0 24px rgba(0,0,0,.15); }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr 56px; }
  .rail { order: 2; flex-direction: row; justify-content: space-around; border-right: 0; border-top: 1px solid var(--line); padding: 0 4px; }
  .rail .logo, .rail .spacer { display: none; }
  .inbox { grid-template-columns: 1fr; }
  .inbox.has-chat .list { display: none; }
  .inbox:not(.has-chat) .conv { display: none; }
  .back { display: inline-flex; }
  .details { width: 100%; }
  .msgs { padding: 12px 10px; }
  .msg { max-width: 88%; }
  .page { padding: 16px; }
  .composer .hint { display: none; }
  .conv-head select.field { max-width: 140px; }
}
