* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }

:root {
  --bg: oklch(0.985 0.003 250);
  --bg-elevated: oklch(1 0 0);
  --bg-sidebar: oklch(0.975 0.005 250);
  --bg-hover: oklch(0.95 0.006 250);
  --border: oklch(0.90 0.007 250);
  --border-soft: oklch(0.94 0.006 250);
  --text: oklch(0.24 0.02 255);
  --text-secondary: oklch(0.48 0.02 255);
  --text-tertiary: oklch(0.64 0.015 255);
  --accent: oklch(0.55 0.19 275);
  --accent-hover: oklch(0.47 0.20 275);
  --accent-soft: oklch(0.93 0.035 275);
  --accent-text: oklch(0.98 0.01 275);
  --success: oklch(0.60 0.14 155);
  --success-soft: oklch(0.93 0.06 155);
  --warning: oklch(0.68 0.15 75);
  --warning-soft: oklch(0.93 0.07 80);
  --danger: oklch(0.58 0.19 25);
  --danger-soft: oklch(0.93 0.06 25);
  --tg: oklch(0.60 0.15 235);
  --tg-soft: oklch(0.92 0.04 235);
  --vk: oklch(0.50 0.18 260);
  --vk-soft: oklch(0.92 0.04 260);
  --max: oklch(0.62 0.10 190);
  --max-soft: oklch(0.92 0.035 190);
  --shadow-card: 0 1px 2px oklch(0.2 0.02 255 / 0.06), 0 1px 8px oklch(0.2 0.02 255 / 0.04);
}
:root[data-theme="dark"] {
  --bg: oklch(0.20 0.012 260);
  --bg-elevated: oklch(0.245 0.014 260);
  --bg-sidebar: oklch(0.17 0.013 260);
  --bg-hover: oklch(0.28 0.015 260);
  --border: oklch(0.33 0.016 260);
  --border-soft: oklch(0.29 0.015 260);
  --text: oklch(0.94 0.006 260);
  --text-secondary: oklch(0.74 0.012 260);
  --text-tertiary: oklch(0.56 0.014 260);
  --accent: oklch(0.68 0.17 275);
  --accent-hover: oklch(0.74 0.16 275);
  --accent-soft: oklch(0.32 0.06 275);
  --accent-text: oklch(0.16 0.02 275);
  --success: oklch(0.68 0.14 155);
  --success-soft: oklch(0.30 0.06 155);
  --warning: oklch(0.75 0.14 80);
  --warning-soft: oklch(0.32 0.07 80);
  --danger: oklch(0.68 0.18 25);
  --danger-soft: oklch(0.32 0.08 25);
  --tg: oklch(0.70 0.13 235);
  --tg-soft: oklch(0.30 0.05 235);
  --vk: oklch(0.66 0.15 260);
  --vk-soft: oklch(0.30 0.05 260);
  --max: oklch(0.70 0.09 190);
  --max-soft: oklch(0.30 0.045 190);
}

body {
  background: var(--bg); color: var(--text);
  font-family: 'Golos Text', system-ui, sans-serif;
  font-size: 14px; line-height: 1.45;
}
h1, .brand-name { font-family: 'Unbounded', system-ui, sans-serif; letter-spacing: -0.01em; }
a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }
button, input { font-family: inherit; }
button { cursor: pointer; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

.app { display: flex; width: 100%; height: 100vh; overflow: hidden; }

.sidebar { width: 248px; flex-shrink: 0; background: var(--bg-sidebar); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 20px 14px; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 22px 8px; }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); display: flex; align-items: center; justify-content: center; flex-shrink: 0; color: var(--accent-text); }
.brand-name { font-size: 16px; font-weight: 700; }

.nav-item { display: flex; align-items: center; gap: 11px; padding: 10px 12px; border-radius: 10px; border: none; background: transparent; color: var(--text-secondary); font-size: 14px; font-weight: 500; text-align: left; width: 100%; }
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item svg { flex-shrink: 0; }
.nav-badge { margin-left: auto; background: var(--accent); color: var(--accent-text); font-size: 11px; font-weight: 700; border-radius: 999px; padding: 1px 7px; }

.sidebar-footer { margin-top: auto; display: flex; align-items: center; gap: 10px; padding: 12px 8px 4px 8px; border-top: 1px solid var(--border); }
.avatar { border-radius: 999px; display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0; }
.op-name { font-size: 13px; font-weight: 600; }
.op-role { font-size: 11.5px; color: var(--text-tertiary); background: none; border: none; padding: 0; cursor: pointer; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 16px 24px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.topbar h1 { font-size: 19px; margin: 0; font-weight: 700; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn { width: 36px; height: 36px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg-elevated); display: flex; align-items: center; justify-content: center; color: var(--text-secondary); }
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.view { flex: 1; min-height: 0; display: none; }
.view.active { display: flex; }

.conv-list { width: 320px; flex-shrink: 0; border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.chip-row { display: flex; gap: 6px; padding: 14px; }
.chip { padding: 5px 11px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text-secondary); }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip.disabled { opacity: 0.5; cursor: default; }
.conv-scroll { flex: 1; overflow-y: auto; }
.conv-row { display: flex; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border-soft); align-items: flex-start; width: 100%; background: transparent; border-left: none; border-right: none; border-top: none; text-align: left; }
.conv-row:hover { background: var(--bg-hover); }
.conv-row.selected { background: var(--accent-soft); }
.conv-body { min-width: 0; flex: 1; }
.conv-top-row { display: flex; align-items: center; gap: 6px; width: 100%; }
.conv-name { font-weight: 600; font-size: 13.5px; }
.conv-time { margin-left: auto; font-size: 11.5px; color: var(--text-tertiary); flex-shrink: 0; }
.conv-preview-row { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
.conv-preview { font-size: 12.5px; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-badge { width: 18px; height: 18px; border-radius: 6px; font-size: 9px; font-weight: 800; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.unread-dot { width: 8px; height: 8px; border-radius: 999px; background: var(--accent); flex-shrink: 0; margin-left: auto; }

.thread { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.thread-header { display: flex; align-items: center; gap: 10px; padding: 14px 20px; border-bottom: 1px solid var(--border); }
.thread-name { font-weight: 700; font-size: 14.5px; }
.thread-sub { font-size: 12px; color: var(--text-tertiary); }
.thread-scroll { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.bubble-row { display: flex; }
.bubble-row.out { justify-content: flex-end; }
.bubble-col { display: flex; flex-direction: column; max-width: 58%; }
.bubble-row.out .bubble-col { align-items: flex-end; }
.bubble { padding: 10px 13px; border-radius: 14px; font-size: 13.5px; white-space: pre-wrap; }
.bubble.in { background: var(--bg-elevated); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.out { background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 4px; }
.bubble-time { font-size: 10.5px; margin-top: 4px; color: var(--text-tertiary); }
.composer { display: flex; align-items: center; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.composer input { flex: 1; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text); font-size: 13.5px; }
.send-btn { width: 38px; height: 38px; border-radius: 10px; background: var(--accent); color: var(--accent-text); border: none; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.send-btn:disabled { opacity: 0.5; cursor: default; }

.panel-pad { padding: 20px 28px; flex: 1; overflow-y: auto; width: 100%; }
.tabs { display: flex; gap: 6px; margin-bottom: 16px; }
.tab { padding: 7px 14px; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--text-secondary); border: 1px solid transparent; background: transparent; }
.tab.active { background: var(--bg-elevated); border-color: var(--border); color: var(--text); box-shadow: var(--shadow-card); }
table { width: 100%; border-collapse: collapse; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-tertiary); padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 700; }
td { padding: 13px 16px; border-bottom: 1px solid var(--border-soft); font-size: 13.5px; vertical-align: middle; }
tr:last-child td { border-bottom: none; }
.user-cell { display: flex; align-items: center; gap: 10px; }
.muted { color: var(--text-tertiary); }
.badge-pill { padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 700; }
select.badge-select { border: none; font-size: 11.5px; font-weight: 700; border-radius: 999px; padding: 3px 22px 3px 9px; appearance: none; -webkit-appearance: none; background-repeat: no-repeat; background-position: right 7px center; }

.kanban { display: flex; gap: 16px; flex: 1; overflow-x: auto; padding: 20px 28px; align-items: flex-start; width: 100%; }
.kcol { width: 300px; flex-shrink: 0; display: flex; flex-direction: column; gap: 10px; }
.kcol-head { display: flex; align-items: center; gap: 8px; padding: 0 4px; }
.kcol-title { font-weight: 700; font-size: 13px; }
.kcol-count { font-size: 11.5px; color: var(--text-tertiary); background: var(--bg-hover); padding: 1px 8px; border-radius: 999px; }
.kcard { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 11px; padding: 13px; display: flex; flex-direction: column; gap: 8px; box-shadow: var(--shadow-card); }
.kcard-id { font-size: 11px; color: var(--text-tertiary); font-weight: 600; }
.kcard-title { font-size: 13px; font-weight: 600; }
.kcard-foot { display: flex; align-items: center; gap: 7px; margin-top: 2px; }
.kcard-user { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }

.channels-grid { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 16px; padding: 24px 28px; width: 100%; }
.channel-card { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 14px; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.channel-card.soon { opacity: 0.75; }
.channel-top { display: flex; align-items: center; gap: 12px; }
.channel-icon { width: 44px; height: 44px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 14px; flex-shrink: 0; }
.channel-name { font-weight: 700; font-size: 14.5px; }
.status-dot-row { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.status-dot { width: 7px; height: 7px; border-radius: 999px; }
.channel-desc { font-size: 12.5px; color: var(--text-secondary); }
.empty-hint { padding: 40px; text-align: center; color: var(--text-tertiary); font-size: 13px; }

/* Login page */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.login-card { width: 340px; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 16px; padding: 32px; box-shadow: var(--shadow-card); }
.login-card h1 { font-size: 18px; margin: 0 0 20px 0; text-align: center; }
.login-card label { font-size: 12.5px; color: var(--text-secondary); font-weight: 600; display: block; margin-bottom: 6px; }
.login-card input { width: 100%; padding: 10px 12px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 13.5px; margin-bottom: 14px; }
.login-card button { width: 100%; padding: 10px; border-radius: 9px; border: none; background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: 13.5px; }
.login-error { color: var(--danger); font-size: 12.5px; margin-bottom: 10px; min-height: 16px; }

.toast-host { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 1000; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 13px; box-shadow: var(--shadow-card); max-width: min(90vw, 420px); text-align: center; animation: toast-in 0.15s ease; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* Bot settings modal */
.modal-backdrop { position: fixed; inset: 0; background: oklch(0.1 0.01 255 / 0.5); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 24px; animation: toast-in 0.12s ease; }
.modal { width: 100%; max-width: 640px; max-height: 88vh; background: var(--bg); border-radius: 16px; border: 1px solid var(--border); box-shadow: var(--shadow-card); display: flex; flex-direction: column; overflow: hidden; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.modal-header h2 { font-size: 16.5px; margin: 0; font-weight: 700; }
.modal-body { padding: 20px 22px; overflow-y: auto; }

.bot-text-card { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 9px; }
.bot-text-area { width: 100%; padding: 9px 11px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg); color: var(--text); font-family: inherit; font-size: 13px; resize: vertical; }

.auto-reply-form { display: grid; grid-template-columns: 1fr 1.4fr auto; gap: 8px; }
.auto-reply-form input { padding: 9px 12px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text); font-size: 13px; }
.auto-reply-row { display: flex; align-items: center; gap: 10px; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 11px; padding: 10px 12px; }
.ar-body { min-width: 0; flex: 1; }
.ar-trigger { font-size: 13px; font-weight: 600; }
.ar-reply { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.switch { position: relative; display: inline-block; width: 36px; height: 21px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch-track { position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: background 0.15s ease; cursor: pointer; }
.switch-track::before { content: ""; position: absolute; width: 16px; height: 16px; left: 2.5px; top: 2.5px; background: white; border-radius: 999px; transition: transform 0.15s ease; box-shadow: 0 1px 2px oklch(0.2 0.02 255 / 0.25); }
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::before { transform: translateX(15px); }

@media (max-width: 860px) {
  .auto-reply-form { grid-template-columns: 1fr; }
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal { max-width: 100%; max-height: 92vh; border-radius: 16px 16px 0 0; }
}

/* Composer attach/emoji popovers */
.popover { position: absolute; left: 14px; bottom: 56px; background: var(--bg-elevated); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow-card); padding: 6px; z-index: 30; animation: toast-in 0.12s ease; }
.menu-item { display: flex; align-items: center; gap: 9px; width: 190px; padding: 9px 10px; border-radius: 8px; border: none; background: transparent; color: var(--text); font-size: 13px; text-align: left; }
.menu-item:hover { background: var(--bg-hover); }
.emoji-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; width: 252px; }
.emoji-item { width: 34px; height: 34px; border-radius: 8px; border: none; background: transparent; font-size: 19px; display: flex; align-items: center; justify-content: center; }
.emoji-item:hover { background: var(--bg-hover); }

/* Composer: attachments + recording */
.icon-ghost { width: 38px; height: 38px; border-radius: 10px; background: transparent; border: 1px solid var(--border); color: var(--text-secondary); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.icon-ghost:hover { background: var(--bg-hover); color: var(--text); }
.bubble.media { padding: 8px; background: transparent; border: none; }
.recorder-bar { gap: 10px; }
.rec-dot { width: 10px; height: 10px; border-radius: 999px; background: var(--danger); flex-shrink: 0; animation: rec-pulse 1.2s ease-in-out infinite; }
@keyframes rec-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
.back-btn { display: none; width: 32px; height: 32px; border-radius: 8px; border: none; background: transparent; color: var(--text-secondary); align-items: center; justify-content: center; flex-shrink: 0; }
.back-btn:hover { background: var(--bg-hover); }

/* Mobile bottom tab bar (hidden on desktop) */
.mobile-tabbar { display: none; }

/* ---------------- Responsive: phones/small tablets ---------------- */
@media (max-width: 860px) {
  .sidebar { display: none; }

  .app { flex-direction: column; height: 100dvh; }
  .main { min-height: 0; }

  .mobile-tabbar {
    display: flex;
    order: 2;
    flex-shrink: 0;
    border-top: 1px solid var(--border);
    background: var(--bg-sidebar);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px)) 4px;
  }
  .mobile-tabbar .nav-item {
    flex-direction: column;
    gap: 3px;
    font-size: 10.5px;
    padding: 6px 4px;
    border-radius: 10px;
    text-align: center;
  }
  .mobile-tabbar .nav-item .nav-badge { position: absolute; margin-left: 14px; margin-top: -14px; }

  .topbar { padding: 12px 14px; gap: 8px; }
  .topbar h1 { font-size: 16px; }

  /* Inbox: show either the conversation list or the open thread, never both */
  #view-inbox { position: relative; overflow: hidden; }
  #view-inbox .conv-list { width: 100%; }
  #view-inbox .thread { position: absolute; inset: 0; background: var(--bg); transform: translateX(100%); transition: transform 0.18s ease; }
  #view-inbox.thread-open .conv-list { display: none; }
  #view-inbox.thread-open .thread { transform: translateX(0); }
  .contact-panel { display: none; }
  .back-btn { display: flex; }

  .chip-row { flex-wrap: wrap; }
  .channels-grid { grid-template-columns: 1fr; padding: 16px; }
  .kanban { padding: 14px 16px; }
  .panel-pad { padding: 14px 16px; }
  table { min-width: 640px; }
  .table-scroll { width: 100%; overflow-x: auto; }
  .composer { padding: 10px 12px; }
  .thread-scroll { padding: 14px; }
}
