:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color-scheme: dark;
  --bg: #0e1621;
  --surface: #17212b;
  --surface-2: #202b36;
  --surface-3: #242f3d;
  --border: #263747;
  --muted: #7f91a4;
  --text: #f1f5f9;
  --telegram: #3390ec;
  --telegram-active: #2b5278;
  --whatsapp: #25d366;
  --danger: #a82b32;
  --shadow: 0 18px 60px rgba(0, 0, 0, .24);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { min-height: 100%; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; }
button { border: 0; border-radius: 10px; padding: .65rem .9rem; cursor: pointer; background: #2b7fd3; color: white; transition: background .14s ease, filter .14s ease, transform .14s ease; }
button:hover { filter: brightness(1.07); }
button:active { transform: translateY(1px); }
button:disabled { cursor: not-allowed; opacity: .52; }
button.secondary { background: #263747; }
button.danger { background: var(--danger); }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(circle at 50% -10%, #21354b 0, var(--bg) 48%); }
.auth-card { width: min(390px, 100%); display: grid; gap: 14px; padding: 26px; border: 1px solid var(--border); border-radius: 18px; background: var(--surface); box-shadow: var(--shadow); }
.auth-card h1, .auth-card p { margin: 0; }
.auth-card p, .auth-card small { color: var(--muted); }
.auth-card label { display: grid; gap: 6px; font-size: .92rem; }
.auth-card input, .composer input, #mock-form input, #mock-form select, .onboarding-form input {
  min-width: 0; border: 1px solid #324457; border-radius: 11px; padding: 12px; background: #111b26; color: var(--text); outline: none;
}
.auth-card input:focus, .composer input:focus, .onboarding-form input:focus { border-color: var(--telegram); box-shadow: 0 0 0 3px rgba(51, 144, 236, .12); }
.brand-mark, .brand-dot { display: grid; place-items: center; background: linear-gradient(145deg, #3b8ff5, #2465cf); font-weight: 800; }
.brand-mark { width: 48px; height: 48px; border-radius: 50%; font-size: 1.25rem; }
.brand-dot { width: 38px; height: 38px; border-radius: 50%; font-size: 1rem; box-shadow: 0 5px 18px rgba(51, 144, 236, .24); }
.error { color: #fecaca !important; background: #451a1a; border-radius: 9px; padding: 9px; }

.shell { width: min(1380px, 100%); margin: auto; padding: 10px 16px 16px; }
.app-header { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 8px; padding: 0 2px; }
.topbar-brand, .topbar-account { display: flex; align-items: center; gap: 10px; min-width: 0; }
#app-title { margin: 0; font-size: 1.2rem; letter-spacing: -.02em; white-space: nowrap; }
.user-name { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #c7d3df; font-size: .86rem; font-weight: 650; }
.icon-button { width: 40px; height: 40px; padding: 0; display: grid; place-items: center; border-radius: 50%; font-size: 1.06rem; background: var(--surface-2) !important; }

.settings-panel { margin-bottom: 10px; padding: 18px; border: 1px solid var(--border); border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); }
.section-heading { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; }
.section-heading h2 { margin: 2px 0 0; }
.settings-titlebar { padding-bottom: 14px; border-bottom: 1px solid var(--border); }
.settings-section { padding: 15px 0; border-bottom: 1px solid rgba(38,55,71,.72); }
.settings-section:last-child { border-bottom: 0; padding-bottom: 0; }
.settings-section h3, .settings-section p { margin: 0; }
.settings-section p { color: var(--muted); margin-top: 4px; font-size: .84rem; }
.settings-section-heading, .account-section { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.settings-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.settings-hint { line-height: 1.45; max-width: 760px; }
.eyebrow { color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-size: .67rem; font-weight: 700; }
.connector-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }
.connector-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--surface-2); padding: 13px 14px; border-radius: 12px; }
.connector-identity { display: flex; align-items: center; gap: 10px; min-width: 0; }
.connector-card strong, .connector-card small { display: block; }
.connector-card small { color: var(--muted); margin-top: 3px; }
.status-dot { flex: 0 0 auto; width: 9px; height: 9px; border-radius: 50%; background: #64748b; }
.status-dot.connected { background: #34d399; box-shadow: 0 0 0 3px rgba(52,211,153,.10); }
.status-dot.error { background: #fb7185; }
.status-dot.connecting, .status-dot.starting { background: #fbbf24; }
.connector-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.onboarding-panel { margin-top: 14px; padding: 18px; border: 1px solid #34485d; border-radius: 14px; background: #111b26; }
.onboarding-heading { display: flex; justify-content: space-between; gap: 14px; align-items: flex-start; }
.onboarding-heading h3, .onboarding-heading p { margin: 0 0 6px; }
.onboarding-heading p { color: var(--muted); }
.auth-qr { display: block; width: min(280px, 90%); height: auto; margin: 18px auto; padding: 8px; background: white; border-radius: 16px; }
.onboarding-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: end; margin-top: 14px; }
.onboarding-form label { grid-column: 1 / -1; color: #cbd5e1; font-size: .92rem; }

.layout { display: grid; grid-template-columns: 390px minmax(0, 1fr); height: min(820px, calc(100dvh - 82px)); min-height: 560px; overflow: hidden; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); }
.conversation-pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; border-right: 1px solid var(--border); background: var(--surface); }
.sidebar-toolbar { flex: 0 0 auto; min-height: 58px; display: flex; align-items: center; gap: 9px; padding: 9px 10px; border-bottom: 1px solid rgba(38,55,71,.78); }
.sidebar-search { min-width: 0; flex: 1; height: 40px; display: flex; align-items: center; gap: 8px; padding: 0 11px; border-radius: 20px; background: var(--surface-2); color: var(--muted); }
.sidebar-search:focus-within { box-shadow: inset 0 0 0 1px rgba(51,144,236,.58); }
.sidebar-search-icon { flex: 0 0 auto; font-size: 1.15rem; line-height: 1; transform: translateY(-1px); }
.sidebar-search input { width: 100%; min-width: 0; border: 0; outline: 0; padding: 0; background: transparent; color: var(--text); font-size: .9rem; }
.sidebar-search input::placeholder { color: #788b9e; }
.count-chip { flex: 0 0 auto; min-width: 28px; height: 28px; padding: 0 8px; border-radius: 999px; display: grid; place-items: center; background: var(--surface-3); color: #aab8c7; font-size: .74rem; }
.list { min-height: 0; overflow: auto; padding: 4px; }
.list.empty { display: grid; place-items: center; padding: 28px; color: var(--muted); text-align: center; }
.conversation { width: 100%; min-height: 72px; display: grid; grid-template-columns: 52px minmax(0, 1fr) auto; align-items: center; gap: 10px; padding: 8px 9px; border: 0; border-radius: 10px; background: transparent; color: var(--text); text-align: left; }
.conversation:hover { background: var(--surface-2); filter: none; }
.conversation.active { background: var(--telegram-active); }
.conversation-avatar, .chat-avatar { display: grid; place-items: center; border-radius: 50%; color: white; font-weight: 760; letter-spacing: -.02em; user-select: none; }
.conversation-avatar { width: 52px; height: 52px; font-size: 1rem; }
.chat-avatar { width: 42px; height: 42px; flex: 0 0 auto; font-size: .9rem; }
.avatar-tone-0 { background: linear-gradient(145deg, #5b9df5, #3475d0); }
.avatar-tone-1 { background: linear-gradient(145deg, #9b70e5, #7251ba); }
.avatar-tone-2 { background: linear-gradient(145deg, #e47d64, #b55347); }
.avatar-tone-3 { background: linear-gradient(145deg, #46b99a, #2b8c75); }
.avatar-tone-4 { background: linear-gradient(145deg, #d58b54, #aa6538); }
.avatar-tone-5 { background: linear-gradient(145deg, #d56e9e, #a94d78); }
.conversation-body { min-width: 0; align-self: stretch; display: flex; flex-direction: column; justify-content: center; gap: 4px; }
.conversation-head, .conversation-preview-row { min-width: 0; display: flex; align-items: center; gap: 6px; }
.conversation-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; font-size: .95rem; }
.conversation-preview { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #8fa1b3; font-size: .82rem; line-height: 1.25; }
.conversation.active .conversation-preview, .conversation.active .conversation-time { color: #d2e1ef; }
.preview-own, .preview-sender { color: #61a8e8; font-weight: 650; }
.conversation.active .preview-own, .conversation.active .preview-sender { color: white; }
.conversation-side { min-width: 44px; height: 54px; display: flex; align-items: flex-end; justify-content: space-between; flex-direction: column; padding: 2px 0; }
.conversation-time { color: #7f91a4; font-size: .71rem; font-weight: 500; white-space: nowrap; }
.conversation-status { margin-right: 3px; color: #67b4f7; font-size: .72rem; }
.unread-badge { min-width: 21px; height: 21px; padding: 0 6px; border-radius: 999px; display: grid; place-items: center; background: var(--telegram); color: white; font-size: .68rem; font-weight: 800; }
.platform-badge { flex: 0 0 auto; display: inline-flex; align-items: center; width: max-content; border-radius: 999px; padding: 2px 6px; font-size: .61rem; font-weight: 760; letter-spacing: .01em; border: 1px solid transparent; }
.platform-badge.telegram { background: rgba(51,144,236,.15); color: #81c2fb; border-color: rgba(51,144,236,.25); }
.platform-badge.whatsapp { background: rgba(37,211,102,.12); color: #76e4a0; border-color: rgba(37,211,102,.22); }
.platform-badge.neutral { background: var(--surface-3); color: #9aacbd; border-color: #344659; }
.conversation.active .platform-badge { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.16); color: white; }

.chat { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: #0e1621; --platform-accent: var(--telegram); }
.chat[data-platform="telegram"] { --platform-accent: var(--telegram); }
.chat[data-platform="whatsapp"] { --platform-accent: var(--whatsapp); }
.chat-header { flex: 0 0 auto; min-height: 58px; display: flex; align-items: center; gap: 10px; padding: 8px 13px; border-bottom: 1px solid var(--border); background: var(--surface); }
.chat-back { display: none; width: 38px; height: 38px; padding: 0; border-radius: 50%; flex: 0 0 auto; font-size: 1.85rem; line-height: 1; }
.chat-identity { min-width: 0; flex: 1; }
.chat-title-row { min-width: 0; display: flex; align-items: center; gap: 7px; }
.chat-title-row h2 { min-width: 0; max-width: min(50vw, 620px); margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .98rem; font-weight: 720; }
.chat-connection { color: #8fa1b3; font-size: .68rem; white-space: nowrap; }
.messages { flex: 1; min-height: 0; overflow: auto; display: flex; flex-direction: column; align-items: stretch; gap: 5px; padding: 14px 18px 10px; scroll-behavior: smooth; background-color: #0e1621; background-image: radial-gradient(circle at 14% 18%, rgba(51,144,236,.045) 0 1px, transparent 1.5px), radial-gradient(circle at 72% 68%, rgba(255,255,255,.025) 0 1px, transparent 1.5px); background-size: 34px 34px, 46px 46px; }
.message { width: fit-content; max-width: min(72%, 680px); padding: 8px 10px 6px; flex: 0 0 auto; border-radius: 11px; box-shadow: 0 1px 2px rgba(0,0,0,.18); line-height: 1.34; overflow-wrap: anywhere; }
.message.incoming { align-self: flex-start; border-bottom-left-radius: 4px; background: #20384e; }
.message.outgoing { align-self: flex-end; border-bottom-right-radius: 4px; background: #182533; }
[data-platform="telegram"] .message.incoming { background: #2b5278; }
[data-platform="whatsapp"] .message.incoming { background: #24513d; }
.message-text { white-space: pre-wrap; }
.message-meta { display: flex; align-items: center; justify-content: flex-end; gap: 4px; min-width: 46px; margin-top: 3px; color: rgba(226,236,246,.68); font-size: .65rem; line-height: 1; white-space: nowrap; }
.message-state { font-weight: 800; }
.message-state.sent { color: #75befd; }
.message-state.pending { color: #a5b5c5; }
.message-state.failed { color: #ff8585; }
.history-more { align-self: center; margin: 5px 0 8px; background: rgba(32,43,54,.94); color: #d3deea; padding: 7px 12px; font-size: .76rem; border: 1px solid #304255; border-radius: 15px; }
.date-separator { display: flex; width: 100%; align-items: center; justify-content: center; color: #b5c2cf; font-size: .69rem; margin: 6px 0; flex: 0 0 auto; }
.date-separator span { padding: 5px 10px; white-space: nowrap; background: rgba(31,48,65,.9); border-radius: 12px; box-shadow: 0 1px 2px rgba(0,0,0,.14); }
.chat-placeholder, .history-loading, .history-error, .history-empty { margin: auto; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 7px; color: var(--muted); min-height: 160px; padding: 24px; }
.chat-placeholder strong, .history-empty strong { color: #d3dee9; }
.chat-placeholder-mark { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 50%; background: var(--surface-3); color: #9db0c3; font-weight: 800; margin-bottom: 3px; }
.history-loading::before { content: ""; width: 22px; height: 22px; border: 2px solid #34495e; border-top-color: var(--platform-accent); border-radius: 50%; animation: spin .8s linear infinite; }
.history-error { color: #fda4af; }
@keyframes spin { to { transform: rotate(360deg); } }

.composer { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; padding: 8px 10px max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--surface); }
.composer input { flex: 1; height: 42px; border: 0; border-radius: 21px; padding: 0 15px; background: var(--surface-2); }
.composer input:focus { box-shadow: inset 0 0 0 1px rgba(51,144,236,.55); }
.composer button { width: 42px; height: 42px; min-width: 42px; padding: 0; display: grid; place-items: center; border-radius: 50%; background: var(--platform-accent); font-size: 0; }
.composer button::after { content: "➤"; font-size: 1rem; transform: translateX(1px); }
.send-label { display: none; }
#mock-form { display: flex; gap: 8px; }
#mock-form input, #mock-form select { flex: 1; }
.devbox { margin-top: 16px; padding: 14px; border: 1px dashed #34485d; border-radius: 12px; }
.devbox summary { cursor: pointer; margin-bottom: 12px; }

@media (max-width: 980px) and (min-width: 761px) {
  .layout { grid-template-columns: 330px minmax(0, 1fr); }
}

@media (max-width: 760px) {
  html, body { height: 100%; }
  body { overflow: hidden; background: var(--bg); }
  .shell { width: 100%; height: 100dvh; padding: 0; display: flex; flex-direction: column; }
  .app-header {
    flex: 0 0 auto;
    min-height: calc(54px + env(safe-area-inset-top));
    margin: 0;
    padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
    align-items: center;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
  }
  .brand-dot { width: 38px; height: 38px; }
  #app-title { display: none; }
  .topbar-account { gap: 8px; margin-left: auto; }
  .user-name { display: block; max-width: 150px; font-size: .82rem; }
  .icon-button { width: 40px; height: 40px; }

  .settings-panel {
    position: fixed;
    z-index: 30;
    top: max(8px, env(safe-area-inset-top));
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    overflow: auto;
    margin: 0;
    padding: 15px;
    border-radius: 15px;
  }
  .settings-titlebar { align-items: center; flex-direction: row; }
  .settings-section-heading, .account-section { align-items: flex-start; }
  .connector-grid { grid-template-columns: 1fr; }
  .connector-card { align-items: flex-start; flex-direction: column; }
  .connector-actions { width: 100%; justify-content: flex-start; }
  .onboarding-heading { flex-direction: column; }
  .onboarding-form { grid-template-columns: 1fr; }
  .onboarding-form label { grid-column: auto; }

  .layout { flex: 1; min-height: 0; height: auto; grid-template-columns: 1fr; border: 0; border-radius: 0; box-shadow: none; }
  .conversation-pane { grid-column: 1; grid-row: 1; border: 0; min-height: 0; }
  .sidebar-toolbar { min-height: 54px; padding: 7px 8px; }
  .sidebar-search { height: 40px; }
  .list { padding: 2px 4px 6px; }
  .conversation { min-height: 72px; grid-template-columns: 50px minmax(0, 1fr) auto; padding: 7px 8px; border-radius: 9px; }
  .conversation-avatar { width: 50px; height: 50px; }
  .conversation-title { font-size: .94rem; }
  .conversation-preview { font-size: .8rem; }
  .platform-badge { font-size: .59rem; padding: 2px 5px; }

  .chat { grid-column: 1; grid-row: 1; min-height: 0; display: none; }
  .layout.chat-open .conversation-pane { display: none; }
  .layout.chat-open .chat { display: flex; }
  .chat-header { min-height: 56px; padding: 7px 8px; }
  .chat-back { display: grid; place-items: center; }
  .chat-avatar { width: 40px; height: 40px; }
  .chat-title-row { gap: 5px; }
  .chat-title-row h2 { max-width: 38vw; font-size: .94rem; }
  .chat-connection { max-width: 22vw; overflow: hidden; text-overflow: ellipsis; font-size: .64rem; }
  .messages { padding: 10px 8px 8px; }
  .message { max-width: 86%; font-size: .93rem; }
  .composer { padding: 7px 8px max(7px, env(safe-area-inset-bottom)); }
  .composer input { height: 40px; }
  .composer button { width: 40px; height: 40px; min-width: 40px; }
  #mock-form { flex-direction: column; }
  .devbox { display: none; }
}

@media (max-width: 430px) {
  .user-name { max-width: 118px; }
  .conversation { grid-template-columns: 48px minmax(0, 1fr) auto; gap: 8px; }
  .conversation-avatar { width: 48px; height: 48px; }
  .chat-title-row h2 { max-width: 34vw; }
  .settings-section-heading { flex-direction: column; }
  .settings-actions { justify-content: flex-start; }
}
