:root {
  --bg: #0b141a;
  --panel: #111b21;
  --panel-2: #182229;
  --line: #222d34;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --out: #005c4b;
  --in: #202c33;
  --danger: #f15c6d;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 13px; min-height: 18px; }
button { cursor: pointer; font: inherit; }
.brand { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.logo { color: var(--accent); }

/* Login */
.login { display: flex; align-items: center; justify-content: center; height: 100%; }
.login-card {
  background: var(--panel); padding: 28px; border-radius: 14px; width: min(360px, 92vw);
  display: flex; flex-direction: column; gap: 12px; border: 1px solid var(--line);
}
.login-card input {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  padding: 12px; border-radius: 8px; font-size: 15px;
}
.login-card button, .composer button, #new-btn {
  background: var(--accent); color: #04221b; border: none; border-radius: 8px;
  padding: 12px; font-weight: 600;
}

/* App layout */
.app { display: grid; grid-template-columns: 360px 1fr; height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px; border-bottom: 1px solid var(--line); }
.head-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn { background: transparent; color: var(--text); border: none; font-size: 20px; line-height: 1; padding: 6px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--panel-2); }
.icon-btn.off { opacity: .4; filter: grayscale(1); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.online { background: var(--accent); }
.dot.stale { background: #d9a441; }

.thread-list { overflow-y: auto; flex: 1; min-height: 0; }
.thread {
  display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line);
  cursor: pointer; align-items: center;
}
.thread:hover { background: var(--panel-2); }
.thread.active { background: var(--panel-2); }
.avatar {
  width: 42px; height: 42px; border-radius: 50%; background: var(--panel-2);
  display: flex; align-items: center; justify-content: center; color: var(--muted);
  font-weight: 600; flex: 0 0 auto; border: 1px solid var(--line);
}
.thread-main { min-width: 0; flex: 1; }
.thread-top { display: flex; align-items: baseline; gap: 8px; }
.thread-top .thread-name { flex: 1; min-width: 0; }
.thread-time { flex: 0 0 auto; font-size: 11px; color: var(--muted); }
.thread-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.account-menu { padding: 12px 14px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.device-status { font-size: 12px; line-height: 1.5; }
.link { background: none; border: none; color: var(--accent); padding: 0; text-align: left; }

/* Conversation */
.conversation { display: flex; flex-direction: column; min-width: 0; min-height: 0;
  background:
    linear-gradient(rgba(11,20,26,.95), rgba(11,20,26,.95)); }
.conv-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.conv-name { font-weight: 600; }
.conv-sub { font-size: 12px; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 18px 12% 18px 12%; display: flex; flex-direction: column; gap: 6px; }
.placeholder { margin: auto; }
.bubble {
  max-width: 70%; padding: 8px 11px; border-radius: 10px; font-size: 14.5px; line-height: 1.35;
  word-wrap: break-word; white-space: pre-wrap; position: relative;
}
.bubble .meta { font-size: 10.5px; color: var(--muted); margin-top: 3px; text-align: right; }
.bubble.out { align-self: flex-end; background: var(--out); }
.bubble.in { align-self: flex-start; background: var(--in); }
.bubble.failed { border: 1px solid var(--danger); }
.tick.failed { color: var(--danger); }

.composer { display: flex; gap: 10px; padding: 12px 16px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; font-size: 15px; max-height: 140px;
}
.composer button { width: 46px; height: 44px; flex: 0 0 auto; font-size: 18px; }

/* Devices overlay */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.sheet { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; width: min(560px, 96vw); max-height: 90vh; overflow-y: auto; padding: 18px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.devices-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.device-row { display: flex; align-items: center; gap: 12px; padding: 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; }
.device-row .meta { flex: 1; min-width: 0; }
.device-row .dname { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.device-row .dsub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.badge { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; background: var(--accent); color: #04221b; padding: 2px 6px; border-radius: 5px; }
.row-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.app-download { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 14px; display: flex; align-items: center; gap: 12px; }
.app-download .dl-meta { flex: 1; min-width: 0; }
.app-download .dl-title { font-weight: 600; }
.app-download .dl-sub { font-size: 12px; color: var(--muted); }
.app-download a.dl-btn { background: var(--accent); color: #04221b; text-decoration: none; border-radius: 8px; padding: 9px 14px; font-weight: 600; white-space: nowrap; }
.primary-btn { background: var(--accent); color: #04221b; border: none; border-radius: 9px; padding: 11px 14px; font-weight: 600; width: 100%; }
.ghost-btn, .mini-btn { background: transparent; color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.mini-btn:hover, .ghost-btn:hover { background: var(--panel); }
.mini-btn.danger { color: var(--danger); border-color: #4a2a30; }
.pair-result { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 16px; }
.pair-result h3 { margin: 0 0 8px; }
.pair-steps { margin: 0 0 12px; padding-left: 20px; color: var(--text); font-size: 14px; line-height: 1.6; }
.pair-label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 4px; }
.pair-code { width: 100%; background: var(--bg); color: var(--accent); border: 1px solid var(--line); border-radius: 8px; padding: 10px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; word-break: break-all; resize: none; }
.pair-actions { display: flex; gap: 8px; margin-top: 10px; }
.pair-actions .primary-btn { width: auto; flex: 1; }
.small { font-size: 12px; }

.new-search { width: 100%; background: var(--panel-2); border: 1px solid var(--line); color: var(--text); border-radius: 9px; padding: 12px; font-size: 15px; margin-bottom: 12px; }

/* Contacts & groups */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; border-bottom: 1px solid var(--line); }
.tab { background: transparent; border: none; color: var(--muted); padding: 8px 14px; font-weight: 600; border-bottom: 2px solid transparent; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.pane { display: block; }
.add-row { display: flex; gap: 8px; margin-bottom: 12px; }
.add-row input { flex: 1; min-width: 0; background: var(--panel-2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 10px; }
.primary-btn.compact { width: auto; padding: 10px 16px; flex: 0 0 auto; }
.import-block { margin-bottom: 12px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
.import-block summary { cursor: pointer; color: var(--accent); font-size: 13px; }
.import-block textarea { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin: 8px 0; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; resize: vertical; }
.people-list { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; }
.person-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; }
.person-row.selectable { cursor: pointer; }
.person-row.selected { border-color: var(--accent); background: #16302a; }
.person-row .pmeta { flex: 1; min-width: 0; }
.person-row .pname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.person-row .pnum { font-size: 12px; color: var(--muted); }
.person-row .check { color: var(--accent); font-weight: 700; width: 18px; text-align: center; }
.empty-note { color: var(--muted); padding: 12px; text-align: center; }

/* Auth toggle (login/signup) */
.auth-toggle { text-align: center; margin-top: 4px; }
.auth-toggle a { color: var(--accent); }

/* Subscribe / usage banner */
.subscribe-banner { display: flex; align-items: center; justify-content: center; gap: 14px;
  background: linear-gradient(90deg, rgba(0,168,132,.18), rgba(0,168,132,.08));
  border-bottom: 1px solid var(--accent); padding: 10px 16px; font-size: 14px; flex-wrap: wrap; }
.subscribe-banner .sb-msg { color: var(--text); }
.banner-btn { background: var(--accent); color: #04221b; border: none; border-radius: 8px;
  padding: 7px 14px; font-weight: 700; font-size: 13px; }

/* Plan picker */
.plans-list { display: flex; flex-direction: column; gap: 10px; margin: 6px 0 4px; }
.plan-row { display: flex; align-items: center; gap: 12px; padding: 14px; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 11px; }
.plan-row.featured { border-color: var(--accent); }
.plan-row.sel { box-shadow: 0 0 0 2px var(--accent); }
.plan-row .plan-meta { flex: 1; min-width: 0; }
.plan-row .plan-name { font-weight: 700; }
.plan-row .plan-sub { font-size: 13px; color: var(--text); margin: 2px 0; }
.plan-row .plan-blurb { font-size: 12px; }
.plan-row .primary-btn.compact { width: auto; }
.plan-row .primary-btn:disabled { opacity: .5; cursor: default; }
#plans-msg { text-align: center; min-height: 18px; margin-top: 8px; }

/* Cloud number */
.cloud-number { margin-bottom: 12px; }
.cloud-number .cn-have, .cloud-number .cn-get { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.cloud-number .cn-get { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.cloud-number .cn-num { font-weight: 700; font-size: 17px; }
.cloud-number .cn-title { font-weight: 600; }
.cloud-number .cn-get .primary-btn.compact { width: auto; flex: 0 0 auto; }
.cloud-number .cn-soon { background: var(--panel-2); border: 1px dashed var(--line); border-radius: 10px; padding: 12px 14px; }
.devices-sub { margin: 4px 0 8px; }

/* Call bar */
.call-bar { position: fixed; left: 50%; transform: translateX(-50%); bottom: 22px; z-index: 60;
  display: flex; align-items: center; gap: 16px; background: var(--panel); border: 1px solid var(--accent);
  border-radius: 14px; padding: 12px 18px; box-shadow: 0 12px 40px rgba(0,0,0,.5); min-width: 280px; max-width: 92vw; }
.call-bar .call-info { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.call-bar #call-who { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.call-bar .call-actions { display: flex; gap: 8px; }
.call-btn-ok { background: var(--accent); color: #04221b; border: none; border-radius: 20px; padding: 9px 16px; font-weight: 700; }
.call-btn-end { background: var(--danger); color: #2a0d11; border: none; border-radius: 20px; padding: 9px 16px; font-weight: 700; }
.call-btn-mini { background: transparent; color: var(--text); border: 1px solid var(--line); border-radius: 20px; padding: 9px 14px; }
#conv-head .conv-title { flex: 1; }

/* Campaign manager additions */
.badge.danger { background: var(--danger); color: #2a0d11; }
.badge.ok { background: var(--accent); color: #04221b; }
.badge:not(.ok):not(.danger) { background: var(--line); color: var(--muted); }
.person-row.opted-out { opacity: .6; }
.person-row .chevron { color: var(--muted); font-size: 22px; flex: 0 0 auto; }
.tpl-prev { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.mini-btn.ok { color: var(--accent); border-color: #1c4a3f; }

.add-col { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.add-col input, .add-col textarea {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 10px; font: inherit; resize: vertical;
}

/* Campaign detail */
.cd-controls { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.cd-status-label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.cd-controls select { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 7px 8px; font: inherit; }

.steps-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.step-row { display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; }
.step-num { width: 24px; height: 24px; flex: 0 0 auto; border-radius: 50%; background: var(--accent); color: #04221b; font-weight: 700; font-size: 13px; display: flex; align-items: center; justify-content: center; margin-top: 2px; }
.step-main { flex: 1; min-width: 0; }
.step-when { font-size: 11px; text-transform: uppercase; letter-spacing: .03em; color: var(--accent); margin-bottom: 3px; }
.step-body { white-space: pre-wrap; word-break: break-word; font-size: 14px; }
.step-add { border: 1px dashed var(--line); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.step-add-head { font-weight: 600; }
.step-add textarea, .step-tpl-row select { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 10px; font: inherit; width: 100%; resize: vertical; }
.step-timing { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; }
.step-timing input { width: 64px; background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px; font: inherit; }
.step-timing select { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px; font: inherit; }

.enroll-tools { display: flex; gap: 8px; margin-bottom: 10px; }
.enroll-tools .new-search { margin-bottom: 0; flex: 1; }
.enroll-tools select { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 9px; padding: 0 10px; font: inherit; flex: 0 0 auto; max-width: 40%; }
#cd-enroll-stats { margin: 10px 0; }

/* Mobile bottom navigation — hidden except on phones */
.bottom-nav { display: none; }
.menu-scrim { display: none; }

/* Tablet (701–900px): keep two panes, narrower sidebar */
@media (max-width: 900px) {
  .app { grid-template-columns: 300px 1fr; }
}

/* Phone (≤700px): single-pane stacked navigation. The thread list is home; a
   conversation or a section takes the whole screen; a fixed, safe-area-aware
   bottom nav switches sections. Hash routing in app.js makes hardware /
   browser back walk this stack instead of leaving the app. */
@media (max-width: 700px) {
  * { -webkit-tap-highlight-color: transparent; }
  body { height: 100dvh; }
  .app { display: flex; flex-direction: column; height: 100dvh; }
  .subscribe-banner { order: -1; }
  .sidebar { flex: 1; min-height: 0; border-right: none;
    padding-bottom: calc(58px + env(safe-area-inset-bottom)); }
  .conversation { display: none; }
  .app.show-conversation .sidebar { display: none; }
  .app.show-conversation .conversation { display: flex; flex: 1; min-height: 0; }
  .app.show-conversation .bottom-nav { display: none; }

  /* touch ergonomics: ≥44px targets, ≥16px inputs (no focus zoom) */
  input, select, textarea,
  .login-card input, .composer textarea, .new-search, .add-row input,
  .add-col input, .add-col textarea, .import-block textarea,
  .step-add textarea, .step-timing input, .step-timing select,
  .step-tpl-row select, .cd-controls select, .enroll-tools select { font-size: 16px; }
  .icon-btn { min-width: 44px; min-height: 44px; }
  .mini-btn, .ghost-btn { min-height: 42px; padding: 10px 12px; }
  .link { min-height: 44px; display: flex; align-items: center; font-size: 15px; }
  .thread { padding: 12px 16px; }
  .tab { padding: 12px 14px; }

  /* conversation: full-screen, compose bar pinned with safe-area padding */
  .messages { padding: 14px 10px; }
  .bubble { max-width: 82%; }
  .back { display: inline-flex; align-items: center; justify-content: center; font-size: 28px; }
  .conv-head { padding: 8px 10px; }
  .composer { padding: 10px 10px calc(10px + env(safe-area-inset-bottom)); }
  .composer button { width: 48px; height: 48px; border-radius: 50%; }

  /* bottom navigation */
  .bottom-nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
    display: flex; background: var(--panel); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom); }
  .nav-item { flex: 1; background: none; border: none; color: var(--muted);
    font-size: 11px; font-weight: 600; display: flex; flex-direction: column;
    align-items: center; gap: 3px; padding: 8px 4px 9px; min-height: 56px; }
  .nav-item.active { color: var(--accent); }
  .nav-ico { font-size: 21px; line-height: 1; filter: grayscale(1) brightness(1.3); }
  .nav-item.active .nav-ico { filter: none; }

  /* overlays become full-screen views (bottom nav stays reachable above them) */
  .overlay { padding: 0; align-items: stretch; }
  .sheet { width: 100%; max-height: none; border-radius: 0; border: none;
    padding: 0 14px calc(72px + env(safe-area-inset-bottom)); }
  .sheet-head { position: sticky; top: 0; z-index: 2; background: var(--panel);
    margin: 0 -14px 14px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
  .people-list { max-height: none; }
  #people-overlay .tabs { display: none; }   /* the bottom nav switches Contacts/Groups */

  /* keep the call bar clear of the bottom nav */
  .call-bar { bottom: calc(70px + env(safe-area-inset-bottom)); }

  /* the account menu becomes the "More" bottom sheet */
  .menu-scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .55); z-index: 65; }
  .account-menu { position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
    background: var(--panel); border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    padding: 16px 18px calc(16px + env(safe-area-inset-bottom));
    box-shadow: 0 -12px 40px rgba(0, 0, 0, .5); }

  /* Rows with multiple action buttons would crush the name/number into a
     sliver. Put the name+number on its own full-width line and let the
     buttons wrap onto the line below. Picker rows (check + name only, no
     buttons) are untouched. */
  .person-row:has(.mini-btn) { flex-wrap: wrap; }
  .person-row:has(.mini-btn) .pmeta { flex-basis: 100%; margin-bottom: 8px; }
  .person-row .pname { white-space: normal; }
  .add-row { flex-wrap: wrap; }
  .add-row input { flex: 1 1 100%; }
  .add-row .primary-btn.compact { width: 100%; }
}
