:root {
  --brand: #1f6f50; --brand-dark: #17573f; --brand-soft: #e6f2ec;
  --bg: #f4f5f3; --panel: #fff; --line: #e3e5e1; --text: #1d2420; --muted: #6b736e;
  --in: #eef0ed; --note: #fff6d9; --bad: #b42318; --warn: #a15c07;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text); background: var(--bg);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); font-size: 14px; margin: 4px 0; min-height: 1em; }
.spacer { flex: 1; }

.btn { border: 1px solid transparent; background: none; padding: 8px 14px; border-radius: 10px; cursor: pointer; text-decoration: none; white-space: nowrap; font-weight: 500; display: inline-flex; align-items: center; gap: 4px; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.ghost { border-color: var(--line); background: var(--panel); }
.btn.ghost:hover { background: var(--bg); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: default; }
.pill { white-space: nowrap; display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--in); color: var(--muted); font-weight: 600; }
.pill.ok { background: var(--brand-soft); color: var(--brand); }
.pill.warn { background: #fdf0d5; color: var(--warn); }

/* layout */
.app { display: grid; grid-template-columns: 340px 1fr auto; grid-template-rows: auto 1fr; height: 100dvh; }
.topbar { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); padding-top: max(10px, env(safe-area-inset-top)); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.list-pane { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.search { padding: 12px 12px 6px; }
.search input, .info-pane input, #lsSearch { width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.tabs { display: flex; gap: 4px; padding: 4px 12px 8px; }
.tab { flex: 1; border: 0; background: none; padding: 7px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--brand-soft); color: var(--brand); }
.conv-list { list-style: none; margin: 0; padding: 0 6px 12px; overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px; border-radius: 12px; cursor: pointer; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--brand-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--in); display: grid; place-items: center; font-weight: 700; color: var(--brand); flex: none; }
.conv-main { min-width: 0; flex: 1; }
.conv-top { display: flex; justify-content: space-between; gap: 6px; }
.conv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { color: var(--muted); font-size: 12px; flex: none; }
.conv-prev { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .conv-name, .conv.unread .conv-prev { color: var(--text); font-weight: 700; }
.dot { display: inline-block; width: 8px; height: 8px; background: var(--brand); border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.none { color: var(--muted); padding: 24px 16px; text-align: center; list-style: none; }

.thread-pane { min-width: 0; min-height: 0; display: flex; }
.empty { flex: 1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }
.empty h2 { color: var(--text); margin: 12px 0 4px; font-size: 20px; }
.thread { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 8px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.who { min-width: 0; }
.who-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who-phone { color: var(--muted); font-size: 13px; }
.back { display: none; font-size: 22px; padding: 2px 12px; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { display: flex; flex-direction: column; max-width: min(75%, 560px); }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.note { align-self: center; max-width: 90%; align-items: center; }
.bubble { padding: 9px 13px; border-radius: var(--radius); line-height: 1.4; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.out .bubble { background: var(--brand); color: #fff; border-bottom-right-radius: 4px; }
.msg.out .bubble a { color: #fff; }
.msg.note .bubble { background: var(--note); font-size: 14px; border-radius: 10px; }
.meta { font-size: 11.5px; color: var(--muted); margin-top: 3px; padding: 0 4px; }
.meta.bad { color: var(--bad); }
.mms { max-width: 240px; max-height: 240px; border-radius: 10px; display: block; margin-bottom: 4px; }
.banner { background: #fdecea; color: var(--bad); padding: 8px 16px; font-size: 14px; }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 12px max(10px, env(safe-area-inset-bottom)); }
.composer.noting { background: var(--note); }
.composer-tools { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 5px 11px; font-size: 13px; cursor: pointer; display: inline-flex; align-items: center; gap: 5px; }
.chip:hover { background: var(--bg); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; max-height: 160px; background: var(--panel); }
.send { padding: 10px 18px; }
.counter { font-size: 12px; color: var(--muted); min-height: 14px; margin-top: 3px; }
.dropdown { position: relative; }
.menu { position: absolute; bottom: calc(100% + 6px); left: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.12); width: min(360px, 85vw); max-height: 300px; overflow-y: auto; z-index: 5; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); font-size: 14px; }
.menu button:hover { background: var(--bg); }
.pad { padding: 12px; }

.info-pane { width: 300px; background: var(--panel); border-left: 1px solid var(--line); padding: 12px 16px; overflow-y: auto; }
.info-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.info-section h4 { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.wo { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; margin: 8px 0; }
.pay { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; padding: 4px 0; }
.matches button { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: none; padding: 8px 4px; cursor: pointer; }
.matches button:hover { background: var(--bg); }

/* dialogs */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(440px, 94vw); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog.wide { width: min(620px, 96vw); }
dialog::backdrop { background: rgba(20, 30, 25, .4); }
.dlg { padding: 20px; max-height: 88dvh; overflow-y: auto; }
.dlg h3 { margin: 0 0 12px; }
.field { display: block; font-size: 14px; font-weight: 600; margin: 10px 0; }
.field input, .field textarea { display: block; width: 100%; margin-top: 5px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; font-weight: 400; background: var(--panel); }
.field input[type=color] { height: 40px; padding: 4px; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; margin: 10px 0; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; position: sticky; bottom: -20px; background: #fff; padding: 10px 0; }
details { border: 1px solid var(--line); border-radius: 12px; padding: 4px 14px; margin: 10px 0; }
summary { cursor: pointer; font-weight: 600; padding: 8px 0; }
.conn { background: var(--bg); border-radius: 12px; padding: 8px 14px; }
.conn-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; flex-wrap: wrap; }
.conn-row > span:first-child { flex: 1; font-weight: 600; font-size: 14px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1d2420; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.2); }
.toast.bad { background: var(--bad); }

/* phones */
@media (max-width: 1100px) { .info-pane { position: fixed; right: 0; top: 0; bottom: 0; z-index: 20; box-shadow: -8px 0 30px rgba(0,0,0,.15); width: min(340px, 92vw); } }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .thread-pane { display: none; }
  body.in-thread .list-pane { display: none; }
  body.in-thread .thread-pane { display: flex; }
  .back { display: inline-flex; }
  .msg { max-width: 85%; }
  .row2 { grid-template-columns: 1fr; }
}

/* login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login form { background: var(--panel); padding: 28px; border-radius: 18px; width: min(360px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.08); text-align: center; }
.login h1 { font-size: 22px; margin: 10px 0 18px; }
.login .btn { width: 100%; justify-content: center; margin-top: 8px; padding: 11px; }
