:root {
  --bg: #ffffff;
  --pane: #f2f2f7;
  --line: #d1d1d6;
  --text: #000000;
  --muted: #8e8e93;
  --blue: #0a84ff;
  --bubble-in: #e9e9eb;
  --bubble-in-text: #000000;
  --bubble-out: #0a84ff;
  --bubble-sys: #34c759;
  --red: #ff3b30;
  --hover: #e5e5ea;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --pane: #1c1c1e;
    --line: #38383a;
    --text: #ffffff;
    --muted: #8e8e93;
    --bubble-in: #26252a;
    --bubble-in-text: #ffffff;
    --hover: #2c2c2e;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.35 var(--font); }
button { font: inherit; }
[hidden] { display: none !important; }

.app { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
.pane { display: flex; flex-direction: column; min-height: 0; }
.list-pane { background: var(--pane); border-right: 1px solid var(--line); }
.thread-pane { background: var(--bg); position: relative; }

.bar { display: flex; align-items: center; gap: 8px; padding: 12px 14px; padding-top: max(12px, env(safe-area-inset-top)); }
.bar h1 { font-size: 28px; margin: 0; flex: 1; font-weight: 700; }
.icon-btn, .text-btn, .back-btn { background: none; border: 0; color: var(--blue); cursor: pointer; padding: 6px; border-radius: 8px; }
.icon-btn:hover, .text-btn:hover { background: var(--hover); }
.text-btn.danger { color: var(--red); }

.search-wrap { padding: 0 14px 8px; }
#search { width: 100%; border: 0; border-radius: 10px; padding: 8px 10px; background: var(--hover); color: var(--text); font: inherit; }
.route-filter { margin: 0 14px 8px; padding: 6px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; }

.conversations { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { display: grid; grid-template-columns: 12px 44px 1fr; gap: 8px; align-items: center; padding: 10px 14px 10px 4px; cursor: pointer; border-bottom: 1px solid var(--line); }
.conv:hover { background: var(--hover); }
.conv.active { background: var(--blue); color: #fff; }
.conv.active .conv-preview, .conv.active .conv-time, .conv.active .conv-route { color: rgba(255,255,255,.85); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--blue); visibility: hidden; }
.conv.unread .dot { visibility: visible; }
.conv.active .dot { background: #fff; }
.conv-main { min-width: 0; }
.conv-top { display: flex; gap: 6px; align-items: baseline; }
.conv-name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { color: var(--muted); font-size: 13px; white-space: nowrap; }
.conv-preview { color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.conv-route { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .02em; }

.avatar { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(#a5a5aa, #85858a); color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 17px; flex: none; }
.avatar.small { width: 32px; height: 32px; font-size: 13px; }

.me { padding: 8px 14px; padding-bottom: max(8px, env(safe-area-inset-bottom)); color: var(--muted); font-size: 12px; border-top: 1px solid var(--line); }

.thread-bar { border-bottom: 1px solid var(--line); background: var(--pane); }
.back-btn { display: none; }
.thread-title { flex: 1; display: flex; gap: 10px; align-items: center; min-width: 0; }
.thread-name { font-weight: 600; }
.thread-sub { color: var(--muted); font-size: 12px; }
.thread-actions { display: flex; gap: 4px; }

.empty-state { margin: auto; color: var(--muted); }

.new-form { display: flex; flex-direction: column; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.new-form label { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.new-form select, .new-form input { flex: 1; font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text); }

.messages { flex: 1; overflow-y: auto; padding: 12px 14px 8px; display: flex; flex-direction: column; gap: 2px; }
.stamp { text-align: center; color: var(--muted); font-size: 12px; margin: 14px 0 6px; }
.row { display: flex; flex-direction: column; }
.row.out { align-items: flex-end; }
.row.in { align-items: flex-start; }
.bubble { max-width: min(75%, 520px); padding: 7px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.row.in .bubble { background: var(--bubble-in); color: var(--bubble-in-text); border-bottom-left-radius: 6px; }
.row.out .bubble { background: var(--bubble-out); color: #fff; border-bottom-right-radius: 6px; }
.row.out.system .bubble { background: var(--bubble-sys); }
.row.dropped .bubble { opacity: .45; }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 12px; margin: 4px 0; }
.bubble a { color: inherit; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px 6px; }
.meta.err { color: var(--red); }

.banner { margin: 0 14px 8px; padding: 8px 12px; border-radius: 10px; background: var(--hover); color: var(--muted); font-size: 13px; text-align: center; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--bg); }
#compose { flex: 1; resize: none; max-height: 160px; border: 1px solid var(--line); border-radius: 18px; padding: 8px 14px; font: inherit; background: var(--bg); color: var(--text); outline: none; }
#compose:focus { border-color: var(--blue); }
.send-btn { width: 32px; height: 32px; border-radius: 50%; border: 0; background: var(--blue); color: #fff; display: grid; place-items: center; cursor: pointer; margin-bottom: 3px; }
.send-btn:disabled { background: var(--line); cursor: default; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .thread-pane { display: none; }
  .app.show-thread .list-pane { display: none; }
  .app.show-thread .thread-pane { display: flex; }
  .back-btn { display: inline-flex; }
  .thread-actions .text-btn { padding: 6px 4px; }
}
