:root {
  --bg: #0f1720; --panel: #17212c; --panel2: #1e2b39; --line: #2a3a4c;
  --text: #e6edf3; --muted: #8b9bab; --accent: #2dd4bf; --accent-ink: #042f2a;
  --ok: #34d399; --warn: #fbbf24; --bad: #f87171; --me: #0f766e; --them: #243447;
  --nav-h: 58px;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #f3f6f9; --panel: #fff; --panel2: #eef2f6; --line: #d9e1e8; --text: #14202b; --muted: #5d6e7e;
    --accent: #0d9488; --accent-ink: #fff; --me: #0d9488; --them: #e4eaf0; }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
#app { height: 100dvh; display: flex; flex-direction: column; max-width: 1400px; margin: 0 auto; }

/* header + nav */
.top { display: flex; align-items: center; gap: 8px; padding: calc(env(safe-area-inset-top) + 10px) 14px 10px; border-bottom: 1px solid var(--line); background: var(--panel); }
.top h1 { font-size: 18px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { background: none; border: 0; padding: 8px 10px; border-radius: 10px; font-size: 20px; line-height: 1; min-width: 44px; min-height: 44px; }
.icon-btn:active { background: var(--panel2); }
main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
nav.tabs { display: flex; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
nav.tabs a { flex: 1; text-align: center; padding: 8px 2px 6px; color: var(--muted); text-decoration: none; font-size: 12px; position: relative; min-height: var(--nav-h); }
nav.tabs a .ic { display: block; font-size: 22px; line-height: 1.2; }
nav.tabs a.on { color: var(--accent); }
.badge { position: absolute; top: 4px; left: calc(50% + 8px); background: var(--bad); color: #fff; font-size: 11px; min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px; line-height: 18px; font-weight: 600; }

/* chips */
.chips { display: flex; gap: 8px; padding: 10px 14px; overflow-x: auto; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 6px 14px; font-size: 14px; color: var(--muted); }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

/* list rows */
.list { display: flex; flex-direction: column; }
.row-wrap { position: relative; overflow: hidden; border-bottom: 1px solid var(--line); }
.row-bg { position: absolute; inset: 0; display: flex; justify-content: space-between; align-items: center; padding: 0 20px; font-weight: 600; color: #fff; }
.row-bg .l { color: #04201c; } .row-bg.r { background: var(--ok); } .row-bg.lft { background: #3b82f6; }
.row { position: relative; background: var(--bg); display: flex; gap: 12px; align-items: center; padding: 12px 14px; min-height: 68px; transition: transform .15s; touch-action: pan-y; text-decoration: none; color: inherit; }
.avatar { flex: none; width: 42px; height: 42px; border-radius: 50%; background: var(--panel2); display: grid; place-items: center; font-weight: 700; color: var(--accent); position: relative; }
.dot { width: 11px; height: 11px; border-radius: 50%; background: var(--muted); border: 2px solid var(--bg); position: absolute; right: -1px; bottom: -1px; }
.dot.on { background: var(--ok); } .dot.off { background: var(--bad); }
.row .body { flex: 1; min-width: 0; }
.row .l1 { display: flex; justify-content: space-between; gap: 8px; }
.row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .time { color: var(--muted); font-size: 12px; flex: none; }
.row .sub { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row.unread .name, .row.unread .sub { color: var(--text); font-weight: 700; }
.unread-pill { flex: none; background: var(--accent); color: var(--accent-ink); border-radius: 999px; min-width: 22px; height: 22px; line-height: 22px; text-align: center; font-size: 12px; font-weight: 700; padding: 0 6px; }
.tag { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 6px; background: var(--panel2); color: var(--muted); margin-right: 6px; }
.tag.bad { background: rgba(248,113,113,.18); color: var(--bad); } .tag.warn { background: rgba(251,191,36,.18); color: var(--warn); } .tag.ok { background: rgba(52,211,153,.16); color: var(--ok); }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; }

/* cards */
.pad { padding: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin-bottom: 12px; }
.card h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 10px; font-size: 14px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; word-break: break-word; }
.bar { height: 8px; border-radius: 4px; background: var(--panel2); overflow: hidden; margin: 4px 0 10px; }
.bar > i { display: block; height: 100%; background: var(--ok); }
.bar > i.warn { background: var(--warn); } .bar > i.bad { background: var(--bad); }
.btn { border: 0; border-radius: 10px; padding: 11px 16px; background: var(--accent); color: var(--accent-ink); font-weight: 600; min-height: 44px; }
.btn.sec { background: var(--panel2); color: var(--text); } .btn.danger { background: var(--bad); color: #fff; }
.btn.sm { padding: 6px 12px; min-height: 34px; font-size: 14px; }
.field { display: block; margin-bottom: 12px; } .field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea { width: 100%; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; min-height: 44px; }
code, .mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.copy { cursor: pointer; background: var(--panel2); border-radius: 6px; padding: 2px 8px; }
.grid2 { display: grid; gap: 12px; grid-template-columns: 1fr; }
canvas.spark { width: 100%; height: 56px; display: block; }

/* thread */
.thread-head { position: relative; }
.thread-head .info { padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); font-size: 14px; display: none; }
.thread-head .info.open { display: block; }
.title-btn { background: none; border: 0; text-align: left; flex: 1; min-width: 0; padding: 0; }
.title-btn .t { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; }
.title-btn .s { color: var(--muted); font-size: 13px; display: block; }
.msgs { flex: 1; overflow-y: auto; padding: 12px 12px 4px; display: flex; flex-direction: column; gap: 6px; touch-action: pan-y; }
.msg { max-width: 82%; padding: 8px 12px; border-radius: 16px; background: var(--them); white-space: pre-wrap; word-break: break-word; }
.msg.admin { align-self: flex-end; background: var(--me); color: #fff; border-bottom-right-radius: 4px; }
.msg.device { align-self: flex-start; border-bottom-left-radius: 4px; }
.msg .meta { display: block; font-size: 11px; opacity: .7; margin-top: 2px; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 8px 0 2px; }
.swipe-hint { align-self: center; color: var(--muted); font-size: 12px; padding: 4px; }
.quick { display: flex; gap: 8px; padding: 6px 12px; overflow-x: auto; scrollbar-width: none; }
.composer { display: flex; gap: 8px; padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px); background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; max-height: 120px; min-height: 44px; border-radius: 22px; padding: 11px 16px; }
.send { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 20px; }

#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px); background: var(--panel2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none; transition: .2s; max-width: 90vw; z-index: 20; }
#toast.show { opacity: 1; transform: translateX(-50%); pointer-events: auto; }

.login { margin: auto; width: min(92vw, 360px); }
.login h1 { text-align: center; }
.conn { font-size: 12px; padding: 4px; text-align: center; background: var(--warn); color: #000; }

/* tablet / desktop: 3-pane */
@media (min-width: 900px) {
  .split { display: grid; grid-template-columns: 360px 1fr 320px; flex: 1; min-height: 0; }
  .split > .pane { overflow-y: auto; border-right: 1px solid var(--line); min-height: 0; display: flex; flex-direction: column; }
  .split > .pane.thread { overflow: hidden; }
  .grid2 { grid-template-columns: 1fr 1fr; }
  .mobile-only { display: none !important; }
}
@media (max-width: 899px) { .desktop-only { display: none !important; } }

.logbox { background: var(--panel2); border-radius: 10px; padding: 10px; max-height: 320px; overflow: auto; margin: 0;
  font: 12px/1.55 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-all; }
.logbox .bad { color: var(--bad); }
.logbox .warn { color: var(--warn, #f59e0b); }

/* attachments */
.attach { flex: none; font-size: 22px; }
.attbar { display: flex; gap: 8px; padding: 8px 12px 0; overflow-x: auto; background: var(--panel); border-top: 1px solid var(--line); }
.attchip { display: flex; align-items: center; gap: 6px; flex: none; max-width: 200px; background: var(--panel2); border-radius: 12px; padding: 4px 6px 4px 4px; font-size: 13px; }
.attchip img { width: 36px; height: 36px; object-fit: cover; border-radius: 8px; }
.attchip > span:first-child { width: 36px; height: 36px; display: grid; place-items: center; }
.attchip .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100px; }
.attchip.uploading { opacity: .6; } .attchip.error { outline: 1px solid var(--bad); }
.attchip button { background: none; border: 0; color: var(--muted); padding: 4px 6px; font-size: 14px; }
.msg .att-img { display: block; max-width: min(240px, 100%); max-height: 260px; border-radius: 12px; margin: 0 0 4px; cursor: zoom-in; background: rgba(0,0,0,.08); }
.msg .att-file { display: flex; align-items: center; gap: 6px; color: inherit; text-decoration: none; background: rgba(0,0,0,.12); border-radius: 10px; padding: 8px 10px; margin: 0 0 4px; max-width: 100%; }
.msg .att-file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .att-file small { opacity: .7; margin-left: auto; flex: none; }
.dropzone { position: fixed; inset: 0; background: color-mix(in srgb, var(--accent) 22%, transparent); border: 3px dashed var(--accent); display: grid; place-items: center; font-size: 22px; font-weight: 700; z-index: 30; pointer-events: none; }
.dropzone[hidden] { display: none; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.88); display: grid; place-items: center; z-index: 40; }
.lightbox img { max-width: 96vw; max-height: 92vh; border-radius: 8px; }
.lightbox button { position: absolute; top: calc(env(safe-area-inset-top) + 10px); right: 10px; color: #fff; }

/* voice call bar */
#callbar { position: fixed; left: 0; right: 0; top: 0; z-index: 30; padding: calc(10px + env(safe-area-inset-top)) 16px 10px; display: flex; align-items: center; gap: 10px; background: var(--me); color: #fff; box-shadow: 0 4px 16px rgba(0,0,0,.35); }
#callbar[hidden] { display: none; }
#callbar.in { background: #1d4ed8; animation: ringpulse 1.2s ease-in-out infinite; }
@keyframes ringpulse { 50% { filter: brightness(1.25); } }
#callbar .who { flex: 1; min-width: 0; line-height: 1.25; }
#callbar .who small { display: block; opacity: .8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#callbar button { border: 0; border-radius: 22px; padding: 10px 16px; font: inherit; font-weight: 600; color: #fff; background: rgba(255,255,255,.22); cursor: pointer; }
#callbar button.ok { background: #16a34a; } #callbar button.no { background: #dc2626; }
