/* =====================================================================
   Feal Inbox - UI styles (light + dark, desktop / tablet / mobile)
   ===================================================================== */
:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #fafbfc;
  --border: #e6e8ec;
  --text: #14161b;
  --muted: #697080;
  --brand: #5b3df5;
  --brand-2: #8b5cf6;
  --brand-soft: #efebff;
  --out-bubble: linear-gradient(135deg, #5b3df5, #8b5cf6);
  --in-bubble: #eef0f4;
  --ig: linear-gradient(135deg, #f58529 0%, #dd2a7b 55%, #8134af 100%);
  --ms: linear-gradient(135deg, #1877f2 0%, #0a9cff 100%);
  --ok: #117a3d;
  --ok-bg: #e3f7ea;
  --warn: #9a5b00;
  --warn-bg: #fff3d6;
  --off: #5b6272;
  --off-bg: #eef0f3;
  --danger: #c0262d;
  --danger-bg: #fdecec;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
  --topbar-h: 60px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1015;
    --panel: #161920;
    --panel-2: #1a1d25;
    --border: #262a34;
    --text: #eceef3;
    --muted: #9aa1ae;
    --brand-soft: #262045;
    --in-bubble: #232733;
    --ok: #4ade80;
    --ok-bg: #11301d;
    --warn: #fbbf24;
    --warn-bg: #362a0b;
    --off: #aab0bd;
    --off-bg: #232733;
    --danger: #f87171;
    --danger-bg: #3a1618;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
body.in-app { overflow: hidden; }
.hidden { display: none !important; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
a { color: var(--brand); }
.ico { width: 18px; height: 18px; flex-shrink: 0; }
h1, h2, h3 { margin: 0; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  padding: 9px 16px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
  white-space: nowrap; text-decoration: none; transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--out-bubble); color: #fff; border: none; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.danger-ghost { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, var(--border)); }
.btn.danger-ghost:hover, .btn.danger-ghost.confirm { background: var(--danger-bg); }
.btn.danger-ghost.confirm { border-color: var(--danger); }
.btn.block { width: 100%; padding: 12px; }
.icon-btn {
  border: none; background: transparent; color: var(--muted); width: 38px; height: 38px; border-radius: 10px;
  display: inline-grid; place-items: center; cursor: pointer; flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 17px; letter-spacing: -.01em; }
.brand-center { justify-content: center; }
.logo { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 10px; background: var(--out-bubble); color: #fff; }
.logo svg { width: 19px; height: 19px; }
.error { color: var(--danger); min-height: 1.2em; margin: 8px 0 0; font-size: 14px; }

/* ---------- segmented control ---------- */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; }
.seg-btn { border: none; background: transparent; padding: 8px 10px; border-radius: 8px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; white-space: nowrap; }
.seg-btn.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.seg-sm .seg-btn { padding: 6px 8px; font-size: 13px; }

/* ---------- auth ---------- */
.auth-view { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 400px; background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 32px 28px; box-shadow: var(--shadow); }
.auth-sub { text-align: center; color: var(--muted); margin: 8px 0 22px; font-size: 14px; }
.auth-card .seg { margin-bottom: 18px; }
#auth-form label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 14px; }
#auth-form input { display: block; width: 100%; margin-top: 6px; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; }
#auth-form input:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.legal-links { text-align: center; font-size: 13px; color: var(--muted); margin: 18px 0 0; }

/* ---------- app shell ---------- */
.app-view { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  height: var(--topbar-h); flex-shrink: 0; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 16px;
  padding: 0 18px; background: var(--panel); border-bottom: 1px solid var(--border); position: relative; z-index: 5;
}
.nav { display: flex; gap: 4px; background: var(--bg); border: 1px solid var(--border); padding: 4px; border-radius: 12px; }
.nav-btn {
  position: relative; display: inline-flex; align-items: center; gap: 8px; padding: 8px 16px; border-radius: 9px;
  color: var(--muted); font-weight: 650; text-decoration: none; transition: background .15s, color .15s;
}
.nav-btn:hover { color: var(--text); }
.nav-btn.active { background: var(--panel); color: var(--brand); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.nav-dot { position: absolute; top: 6px; right: 8px; width: 8px; height: 8px; border-radius: 50%; background: #ef4444; box-shadow: 0 0 0 2px var(--panel); }
.nav-count { font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: var(--ok-bg); color: var(--ok); }
.nav-count.zero { background: var(--warn-bg); color: var(--warn); }
.user-box { justify-self: end; display: flex; align-items: center; gap: 10px; min-width: 0; }
.user-avatar { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); font-weight: 700; font-size: 13px; flex-shrink: 0; }
.user-name { font-weight: 600; color: var(--muted); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.view { flex: 1; min-height: 0; }

/* ---------- inbox ---------- */
.inbox { display: grid; grid-template-columns: 340px 1fr; }
.contacts { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.contacts-head { padding: 16px 16px 12px; display: grid; gap: 10px; border-bottom: 1px solid var(--border); }
.contacts-title { display: flex; align-items: center; justify-content: space-between; }
.contacts-title h2 { font-size: 18px; }
.poll { width: 8px; height: 8px; border-radius: 50%; background: var(--border); transition: background .2s; }
.poll.active { background: var(--ok); }
.search { display: flex; align-items: center; gap: 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 0 10px; color: var(--muted); }
.search input { flex: 1; border: none; background: transparent; padding: 9px 0; font: inherit; color: var(--text); min-width: 0; }
.search input:focus { outline: none; }
.search:focus-within { border-color: var(--brand); }
.contact-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.contact { display: flex; gap: 12px; align-items: center; padding: 10px; border-radius: 12px; cursor: pointer; }
.contact:hover, .contact:focus { background: var(--bg); outline: none; }
.contact.active { background: var(--brand-soft); }
.contact-meta { min-width: 0; flex: 1; }
.contact-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.contact-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.contact-preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 3px; }
.empty { flex: 1; align-content: center; padding: 28px 20px; text-align: center; color: var(--muted); font-size: 14px; line-height: 1.5; display: grid; gap: 6px; justify-items: center; }
.empty strong { color: var(--text); font-size: 15px; }
.empty p { margin: 0 0 8px; }

.avatar-wrap { position: relative; flex-shrink: 0; width: 46px; height: 46px; }
.avatar-wrap.sm { width: 40px; height: 40px; }
.avatar { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; display: block; }
.avatar-fallback { display: grid; place-items: center; background: var(--out-bubble); color: #fff; font-weight: 700; font-size: 14px; }
.ch-badge { position: absolute; right: -2px; bottom: -2px; width: 19px; height: 19px; border-radius: 50%; display: grid; place-items: center; color: #fff; box-shadow: 0 0 0 2px var(--panel); }
.ch-badge svg { width: 11px; height: 11px; }
.ch-instagram { background: var(--ig); }
.ch-messenger { background: var(--ms); }

.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg); }
.chat-empty { flex: 1; display: grid; place-content: center; justify-items: center; text-align: center; color: var(--muted); padding: 24px; gap: 6px; }
.chat-empty h3 { color: var(--text); font-size: 17px; margin-top: 8px; }
.chat-empty p { margin: 0; max-width: 320px; line-height: 1.5; }
.empty-illus { width: 64px; height: 64px; border-radius: 20px; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; }
.empty-illus svg { width: 30px; height: 30px; }
.chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head { display: flex; gap: 12px; align-items: center; padding: 10px 18px; background: var(--panel); border-bottom: 1px solid var(--border); min-height: 64px; }
.chat-head-text { min-width: 0; }
.chat-title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub { display: flex; align-items: center; gap: 8px; margin-top: 3px; font-size: 12px; color: var(--muted); }
.ch-tag { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px 2px 6px; border-radius: 999px; color: #fff; font-weight: 650; font-size: 11px; }
.ch-tag .ico { width: 12px; height: 12px; }
.chat-sub-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back-btn { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 18px 22px; display: flex; flex-direction: column; gap: 4px; }
.day-sep { align-self: center; font-size: 12px; font-weight: 600; color: var(--muted); background: var(--panel); border: 1px solid var(--border); padding: 3px 10px; border-radius: 999px; margin: 10px 0; text-transform: capitalize; }
.msg { display: flex; flex-direction: column; max-width: min(70%, 560px); }
.msg.in { align-self: flex-start; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 9px 13px; border-radius: 18px; line-height: 1.42; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.msg.out .bubble { background: var(--out-bubble); color: #fff; border-bottom-right-radius: 6px; }
.msg-time { font-size: 11px; color: var(--muted); margin: 3px 6px 8px; }
.composer-note { padding: 10px 18px; background: var(--warn-bg); color: var(--warn); font-size: 13px; font-weight: 600; }
.composer { display: flex; gap: 10px; padding: 12px 16px; background: var(--panel); border-top: 1px solid var(--border); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; padding: 11px 16px; border-radius: 22px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font: inherit; max-height: 160px; line-height: 1.4; }
.composer textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.send-btn { border-radius: 22px; padding: 11px 18px; }
.composer-error { padding: 0 16px 10px; margin: 0; background: var(--panel); }
.composer-error:empty { display: none; }

/* ---------- connections ---------- */
.connections { overflow-y: auto; }
.conn-wrap { max-width: 1040px; margin: 0 auto; padding: 36px 24px 48px; }
.conn-head h1 { font-size: 28px; letter-spacing: -.02em; }
.conn-head p { color: var(--muted); margin: 8px 0 0; max-width: 620px; line-height: 1.5; }
.conn-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; margin-top: 28px; }
.conn-card { background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 22px; display: flex; flex-direction: column; gap: 18px; box-shadow: var(--shadow); }
.conn-card-top { display: flex; gap: 14px; align-items: flex-start; }
.ch-icon { width: 48px; height: 48px; border-radius: 14px; display: grid; place-items: center; color: #fff; flex-shrink: 0; }
.ch-icon svg { width: 24px; height: 24px; }
.conn-title { flex: 1; min-width: 0; }
.conn-title h2 { font-size: 18px; }
.conn-title p { margin: 4px 0 0; color: var(--muted); font-size: 14px; line-height: 1.45; }
.status-pill { flex-shrink: 0; font-size: 12px; font-weight: 700; padding: 5px 10px; border-radius: 999px; display: inline-flex; align-items: center; gap: 6px; }
.status-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-pill[data-status="on"] { background: var(--ok-bg); color: var(--ok); }
.status-pill[data-status="off"] { background: var(--off-bg); color: var(--off); }
.status-pill[data-status="warn"] { background: var(--warn-bg); color: var(--warn); }
.conn-details { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 10px 16px; padding: 14px 16px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; font-size: 14px; }
.conn-details dt { color: var(--muted); }
.conn-details dd { margin: 0; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.page-inline { display: inline-flex; align-items: center; gap: 8px; }
.page-pic { width: 26px; height: 26px; border-radius: 7px; object-fit: cover; flex-shrink: 0; }
.page-pic-fallback { display: inline-grid; place-items: center; background: var(--ms); color: #fff; font-size: 11px; font-weight: 700; }
.wh-status { font-weight: 650; }
.wh-checking { color: var(--muted); }
.wh-ok { color: var(--ok); }
.wh-bad { color: var(--danger); }
.conn-note { font-size: 13px; background: var(--warn-bg); color: var(--warn); padding: 10px 12px; border-radius: 10px; font-weight: 600; }
.conn-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: auto; }
.conn-perms { display: flex; gap: 8px; align-items: flex-start; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 14px; line-height: 1.5; overflow-wrap: anywhere; }
.conn-perms .ico { width: 15px; height: 15px; margin-top: 1px; }
.conn-foot { margin-top: 28px; font-size: 13px; color: var(--muted); text-align: center; }

.page-picker { display: grid; gap: 12px; }
.picker-title { margin: 0; font-weight: 650; }
.page-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; max-height: 280px; overflow-y: auto; }
.page-option { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; cursor: pointer; }
.page-option:hover { background: var(--panel-2); }
.page-option.selected { border-color: var(--brand); background: var(--brand-soft); }
.page-option input { accent-color: var(--brand); }
.page-option .page-pic { width: 36px; height: 36px; border-radius: 9px; }
.page-text { display: grid; min-width: 0; }
.page-text small { color: var(--muted); }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 12px 18px; border-radius: 12px; font-weight: 600; max-width: calc(100% - 32px); z-index: 50; box-shadow: 0 8px 24px rgba(0,0,0,.2);
}
.toast.error-toast { background: var(--danger); color: #fff; }

/* ---------- tablet ---------- */
@media (max-width: 1024px) {
  .inbox { grid-template-columns: 290px 1fr; }
  .msg { max-width: 80%; }
  .user-name { display: none; }
}
@media (max-width: 860px) {
  .conn-grid { grid-template-columns: 1fr; }
}

/* ---------- mobile: list OR chat, full screen ---------- */
@media (max-width: 760px) {
  :root { --topbar-h: auto; }
  .topbar {
    grid-template-columns: 1fr auto; gap: 10px; padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top));
  }
  .brand { font-size: 16px; }
  .logo { width: 30px; height: 30px; }
  .nav { grid-column: 1 / -1; grid-row: 2; display: grid; grid-template-columns: 1fr 1fr; }
  .nav-btn { justify-content: center; padding: 9px 10px; }
  .user-box { grid-column: 2; grid-row: 1; }

  .inbox { display: flex; flex-direction: column; }
  .contacts { flex: 1; border-right: none; }
  .chat { display: none; flex: 1; }
  .app-view.chat-open .topbar, .app-view.chat-open .contacts { display: none; }
  .app-view.chat-open .chat { display: flex; }

  .back-btn { display: inline-grid; margin-left: -6px; }
  .chat-head { gap: 8px; padding: 8px 10px; padding-top: calc(8px + env(safe-area-inset-top)); min-height: 58px; }
  .messages { padding: 12px 10px; }
  .msg { max-width: 86%; }
  .composer { padding: 8px 10px; padding-bottom: calc(8px + env(safe-area-inset-bottom)); gap: 8px; }
  .composer textarea { font-size: 16px; }
  .send-btn span { display: none; }
  .send-btn { width: 44px; height: 44px; padding: 0; border-radius: 50%; }

  .conn-wrap { padding: 20px 14px calc(32px + env(safe-area-inset-bottom)); }
  .conn-head h1 { font-size: 24px; }
  .conn-card { padding: 18px; gap: 16px; }
  .conn-card-top { flex-wrap: wrap; }
  .conn-title { flex-basis: calc(100% - 62px); }
  .status-pill { margin-left: 62px; }
  .conn-actions .btn { flex: 1; }

  .auth-card { padding: 24px 18px; }
  #auth-form input { font-size: 16px; }
  .search input { font-size: 16px; }
  .toast { bottom: calc(16px + env(safe-area-inset-bottom)); }
}
@media (max-width: 360px) {
  .seg-sm .seg-btn { font-size: 12px; padding: 6px 4px; }
  .nav-btn { gap: 6px; font-size: 14px; }
}

/* ---------- legal documents ---------- */
body.doc { background: var(--bg); }
.doc-main { max-width: 820px; margin: 0 auto; padding: 40px 16px 60px; line-height: 1.65; }
.doc-main h1 { font-size: 30px; margin-bottom: 4px; }
.doc-main h2 { font-size: 20px; margin-top: 32px; }
.doc-main .updated { color: var(--muted); margin-top: 0; }
.doc-main code { background: var(--in-bubble); padding: 2px 6px; border-radius: 6px; }
.doc-main table { border-collapse: collapse; width: 100%; font-size: 14px; }
.doc-main th, .doc-main td { border: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: top; }
