*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg-0: #0a0a0b;
  --bg-1: #101012;
  --bg-2: #16161a;
  --bg-3: #1c1c20;
  --border: #1f1f22;
  --border-strong: #2a2a2d;
  --text-0: #fafafa;
  --text-1: #e5e5e5;
  --text-2: #d8d8db;
  --text-3: #bcbcc1;
  --text-4: #a4a4aa;
  --brand: #7F77DD;
  --brand-bg: #1a1830;
  --brand-border: #2d2a55;
  --green: #5DCAA5;
  --green-bg: #0f2418;
  --green-border: #1c4030;
  --amber: #EF9F27;
  --amber-bg: #2a1a08;
  --amber-border: #4a2d10;
  --red: #F09595;
  --red-bg: #2a0d0d;
  --red-border: #4a1818;
}

:root[data-theme="light"] {
  --bg-0: #fafafa;
  --bg-1: #ffffff;
  --bg-2: #f4f4f5;
  --bg-3: #e8e8eb;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --text-0: #18181b;
  --text-1: #27272a;
  --text-2: #52525b;
  --text-3: #71717a;
  --text-4: #a1a1aa;
  --brand: #5b54b8;
  --brand-bg: #ece9ff;
  --brand-border: #c7c0f0;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --green-border: #86efac;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --amber-border: #fcd34d;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --red-border: #fca5a5;
}

html, body, #root {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  font-size: 13px;
  line-height: 1.5;
  background: var(--bg-0);
  color: var(--text-1);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  outline: none;
}

input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  outline: none;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

.app {
  display: grid;
  grid-template-columns: 200px 1fr;
  /* Cap the single row at the viewport height (NOT max-content). A bare row grows
     to its tallest child, so a long sidebar/main spilled past the screen and the
     sidebar footer (sign-out, version) got clipped. minmax(0, 1fr) pins it to the
     viewport so each column scrolls INTERNALLY instead. */
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background: var(--bg-1);
  border-right: 0.5px solid var(--border);
  padding: 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  /* The whole sidebar scrolls as ONE column. Previously only the nav list
     scrolled while the bottom cluster stayed pinned — on shorter windows the
     tail items («Настройки»/«Люди»/«⚙ Админ») were silently clipped and looked
     like a dead button. */
  overflow-y: auto;
  min-height: 0;
}
/* Children must not be squashed to fit — they keep natural height and the
   sidebar scrolls instead (otherwise flexbox shrinks them and nothing scrolls). */
.sidebar > * { flex-shrink: 0; }

.sidebar-brand {
  padding: 6px 10px 16px;
  font-weight: 500;
  color: var(--text-0);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--text-3);
  cursor: pointer;
  font-size: 13px;
  transition: background 0.1s, color 0.1s;
  background: transparent;
  width: 100%;
  text-align: left;
}
.nav-item:hover { background: var(--bg-2); color: var(--text-1); }
.nav-item.active { background: var(--bg-3); color: var(--text-0); }

.main { overflow: auto; padding: 18px 24px; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  padding-bottom: 12px;
  border-bottom: 0.5px solid var(--border);
}
.page-title {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-0);
}

.btn {
  background: var(--bg-2);
  border: 0.5px solid var(--border-strong);
  color: var(--text-1);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.1s, border-color 0.1s;
}
.btn:hover { background: var(--bg-3); border-color: #3a3a3e; }
.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}
.btn-primary:hover { background: #6a63c5; border-color: #6a63c5; }
.btn-danger { color: var(--red); border-color: var(--red-border); }
.btn-danger:hover { background: var(--red-bg); }
.btn-icon { padding: 6px 8px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.card {
  background: var(--bg-1);
  border: 0.5px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 10px;
}

.row { display: flex; align-items: center; gap: 12px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.col { display: flex; flex-direction: column; gap: 6px; }

.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

/* Sender page: two columns on wide screens — LEFT = live (progress + console),
   RIGHT = setup (advanced run + pacing + accounts) — so nothing important is buried
   at the bottom of a long single column ("консоль в жопе"). One column when narrow.
   min-width:0 lets wide console/account content shrink instead of stretching the
   track. */
.sender-cols { display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; align-items: start; }
.sender-col-live, .sender-col-setup { min-width: 0; }
@media (max-width: 1000px) { .sender-cols { grid-template-columns: 1fr; } }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
  border: 0.5px solid;
}
.status-pill .dot { width: 6px; height: 6px; border-radius: 50%; }
.status-active { background: var(--green-bg); color: var(--green); border-color: var(--green-border); }
.status-active .dot { background: var(--green); }
.status-paused { background: var(--bg-3); color: var(--text-3); border-color: var(--border-strong); }
.status-paused .dot { background: var(--text-4); }
.status-flood { background: var(--amber-bg); color: var(--amber); border-color: var(--amber-border); }
.status-flood .dot { background: var(--amber); }
.status-banned, .status-dead {
  background: var(--red-bg); color: var(--red); border-color: var(--red-border);
}
.status-banned .dot, .status-dead .dot { background: var(--red); }
.status-reserve { background: var(--bg-3); color: var(--text-4); border-color: var(--border-strong); }
.status-reserve .dot { background: var(--text-4); }

.muted { color: var(--text-4); font-size: 11px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 11px; }

.input {
  background: var(--bg-2);
  border: 0.5px solid var(--border-strong);
  color: var(--text-1);
  padding: 6px 10px;
  border-radius: 6px;
  width: 100%;
}
.input:focus { border-color: var(--brand); }

.label {
  font-size: 11px;
  color: var(--text-4);
  margin-bottom: 4px;
  display: block;
}

.metric {
  background: var(--bg-1);
  border: 0.5px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
}
.metric-label { font-size: 10px; color: var(--text-4); }
.metric-value { font-size: 20px; font-weight: 500; color: var(--text-0); margin-top: 2px; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal {
  background: var(--bg-1);
  border: 0.5px solid var(--border-strong);
  border-radius: 10px;
  padding: 18px;
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  overflow: auto;
}
.modal-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-0);
  margin-bottom: 14px;
}

.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}
.toast {
  background: var(--bg-1);
  border: 0.5px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 12px;
  color: var(--text-1);
  min-width: 240px;
  max-width: 320px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
}
.toast.success { border-left: 2px solid var(--green); }
.toast.error { border-left: 2px solid var(--red); }
.toast.info { border-left: 2px solid var(--brand); }

.account-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 14px;
  align-items: center;
}
.account-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--brand-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-2);
  font-weight: 500;
  font-size: 13px;
}

.empty {
  text-align: center;
  padding: 40px;
  color: var(--text-4);
}

.qr-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  background: var(--bg-2);
  border-radius: 8px;
  margin: 10px 0;
}
.qr-image {
  width: 220px;
  height: 220px;
  background: white;
  padding: 10px;
  border-radius: 6px;
}

.account-header-stats {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--text-4);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

textarea.input { font-family: ui-monospace, Menlo, monospace; font-size: 12px; line-height: 1.5; resize: vertical; }

/* ---- Inbox composer (clean, Telegram-like pill) ---- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: var(--bg-2);
  border: 0.5px solid var(--border-strong);
  border-radius: 22px;
  padding: 4px 6px;
  transition: border-color 0.12s;
}
.composer:focus-within { border-color: var(--brand); }
.composer-actions { display: flex; align-items: center; gap: 1px; padding-bottom: 2px; }
.composer-icon {
  background: transparent;
  border: none;
  color: var(--text-3);
  font-size: 17px;
  line-height: 1;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s, color 0.1s;
}
.composer-icon:hover:not(:disabled) { background: var(--bg-3); color: var(--text-1); }
.composer-icon:disabled { opacity: 0.4; cursor: not-allowed; }
.composer-icon.active { color: var(--brand); }
.composer-textarea {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-0);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  padding: 8px 6px;
  resize: none;
  min-height: 38px;
  max-height: 140px;
  overflow-y: auto;
}
.composer-textarea::placeholder { color: var(--text-4); }
.composer-send {
  background: var(--brand);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: background 0.1s, transform 0.08s;
}
.composer-send:hover:not(:disabled) { background: #6a63c5; }
.composer-send:active:not(:disabled) { transform: scale(0.92); }
.composer-send:disabled { opacity: 0.5; cursor: not-allowed; }
.composer-send.danger { background: var(--red); }
.composer-emoji-panel {
  margin-bottom: 8px;
  padding: 8px;
  background: var(--bg-2);
  border: 0.5px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  max-height: 150px;
  overflow: auto;
}
.composer-emoji-panel button {
  background: transparent;
  border: none;
  font-size: 20px;
  line-height: 1.2;
  padding: 3px 5px;
  border-radius: 6px;
  transition: background 0.1s;
}
.composer-emoji-panel button:hover { background: var(--bg-3); }

.events-feed {
  font-size: 11px;
  font-family: ui-monospace, Menlo, monospace;
  background: var(--bg-2);
  border-radius: 6px;
  padding: 8px 10px;
  max-height: 220px;
  overflow: auto;
  border: 0.5px solid var(--border);
}
.events-feed .ev { color: var(--text-3); margin-bottom: 2px; line-height: 1.5; }
.events-feed .ev .t { color: var(--text-4); margin-right: 6px; }
.events-feed .ev .type { color: var(--brand); margin-right: 6px; }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(1.2); }
}

/* ---- Elegant thin scrollbars (overlay style, fade in on hover) ---- */
* {
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
*:hover {
  scrollbar-color: var(--border-strong) transparent;
}
*::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
*::-webkit-scrollbar-track {
  background: transparent;
}
*::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 8px;
  border: 2px solid transparent;
  background-clip: padding-box;
  transition: background 0.2s;
}
/* Show the thumb only when hovering the scrollable area */
*:hover::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  background-clip: padding-box;
}
*::-webkit-scrollbar-thumb:hover {
  background: var(--text-4);
  background-clip: padding-box;
}
*::-webkit-scrollbar-corner {
  background: transparent;
}

/* =====================================================================
   MOBILE / RESPONSIVE
   Everything below is additive. It only takes effect ≤768px (and ≤480px
   for the tightest tweaks), so the desktop layout above is untouched.
   The hamburger toggle is the one new element that exists on every
   viewport but is hidden on desktop.
   ===================================================================== */

/* Hamburger toggle + drawer backdrop are hidden on desktop. */
.nav-toggle { display: none; }
.nav-backdrop { display: none; }
/* Desktop "reopen sidebar" button — only shown when collapsed (rule below). */
.nav-reopen { display: none; }
/* Conversation info/actions toggle — desktop shows the info as a fixed side column,
   so the toggle is mobile-only (rule in the ≤768px block reveals it). */
.conv-info-toggle { display: none; }

/* Collapsible help notes in Settings — hide the native ▸ marker (we draw our own ⓘ). */
.help-note > summary { list-style: none; }
.help-note > summary::-webkit-details-marker { display: none; }
.help-note > summary:hover { color: var(--text-1); }

/* Collapse button inside the sidebar brand row (desktop sidebar collapse only —
   on mobile the off-canvas drawer + hamburger handle showing/hiding). */
.sidebar-collapse {
  display: none;
  margin-left: auto;
  background: transparent;
  border: 0.5px solid var(--border);
  color: var(--text-3);
  cursor: pointer;
  border-radius: 6px;
  width: 22px;
  height: 22px;
  line-height: 1;
  font-size: 13px;
}
.sidebar-collapse:hover { background: var(--bg-2); color: var(--text-1); }

/* Desktop only: a collapsed sidebar reclaims its column; a small floating
   button brings it back. On mobile the off-canvas drawer handles this instead,
   so these rules are scoped above the mobile breakpoint. */
@media (min-width: 769px) {
  .sidebar-collapse { display: inline-flex; align-items: center; justify-content: center; }
  /* Single column when collapsed: the sidebar is display:none (removed from grid
     flow), so a "0 1fr" template would auto-place .main into the 0-width FIRST
     track and leave 1fr empty — the content collapsed to 0px and you saw the bare
     black .app background ("чёрное окно"). One 1fr column makes .main fill it. */
  .app.nav-collapsed { grid-template-columns: 1fr; }
  .app.nav-collapsed > .sidebar { display: none; }
  .app.nav-collapsed .nav-reopen {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 30;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    background: var(--bg-2);
    border: 0.5px solid var(--border);
    color: var(--text-1);
    cursor: pointer;
    font-size: 15px;
  }
  .app.nav-collapsed .nav-reopen:hover { background: var(--bg-3); }
}
/* Mobile-only "back to list" button inside the inbox thread pane. */
.inbox-back { display: none; }

@media (max-width: 768px) {
  /* --- 1. Off-canvas nav drawer --- */
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 240px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    overflow-y: auto;
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.35);
  }
  .app.nav-open .sidebar { transform: translateX(0); }

  .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 40;
  }

  .nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 8px;
    left: 8px;
    z-index: 45;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--bg-2);
    border: 0.5px solid var(--border-strong);
    color: var(--text-1);
    font-size: 18px;
    line-height: 1;
  }
  .nav-toggle:hover { background: var(--bg-3); }

  /* --- 2. Content density --- */
  /* Push content clear of the fixed hamburger button. */
  .main { padding: 14px 12px; padding-top: 52px; }
  .grid-4, .grid-3 { grid-template-columns: 1fr 1fr; }

  /* --- 3. Modals --- */
  .modal {
    max-width: 96vw;
    width: 96vw;
    max-height: 92vh;
    padding: 14px;
  }

  /* --- 4. Wide tables scroll horizontally instead of overflowing --- */
  .main table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
    max-width: 100%;
  }

  /* --- 5. Inbox: collapse the two-pane into a single pane --- */
  /* Stack the list and thread; the thread is full-width and only shown once a
     chat is open (.thread-open), otherwise the list takes the whole screen. */
  .inbox-grid {
    grid-template-columns: 1fr !important;
    grid-template-rows: 1fr;
  }
  .inbox-grid .inbox-list { display: flex; }
  .inbox-grid .inbox-thread { display: none; }
  .inbox-grid.thread-open .inbox-list { display: none; }
  .inbox-grid.thread-open .inbox-thread {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }
  .inbox-back {
    display: inline-flex;
    align-items: center;
    align-self: flex-start;
    gap: 4px;
    margin-bottom: 8px;
    min-height: 40px;
    padding: 6px 12px;
    background: var(--bg-2);
    border: 0.5px solid var(--border-strong);
    border-radius: 6px;
    color: var(--text-1);
    font-size: 13px;
  }
  /* Inside a thread, collapse the chat + 220px info column into ONE column. Make
     the conversation a flex column: the chat (messages + composer) fills the screen
     and scrolls, the info/actions panel sits below it (capped so it never eats the
     whole view). Previously the grid kept content-sized rows, so the message list
     was squeezed to a couple of lines — "не вижу половину диалога". */
  .inbox-grid.thread-open .inbox-thread > .conv-grid {
    display: flex !important;
    flex-direction: column;
    flex: 1;
    min-height: 0;
  }
  .conv-grid > div:first-child {     /* chat column: messages + composer */
    flex: 1 1 auto;
    min-height: 0;                    /* lets the inner message list (overflow:auto) scroll */
  }
  /* The info/actions panel is COLLAPSED by default on mobile so the message list
     gets the whole screen. The ⚙ header button opens it (.info-open). When open it
     takes up to ~half the screen and scrolls; the chat shrinks to make room.
     !important is required because the panel carries an inline `display:flex`. */
  .conv-grid > div:last-child { display: none !important; }
  .conv-grid.info-open > div:last-child {
    display: flex !important;
    flex: 0 0 auto;
    max-height: 48vh;
    overflow: auto;
    margin-top: 8px;
  }
  /* Reveal the toggle on mobile. */
  .conv-info-toggle { display: inline-flex; align-items: center; justify-content: center; min-height: 32px; }

  /* When a dialog is OPEN on mobile, hide the inbox list's header (Inbox · N unread,
     Mark all read, Import pinned) and the filter chips, so the conversation gets the
     full screen — Telegram-style. The «‹ К списку» button inside the thread is the
     way back. */
  .page-fullh.thread-active .page-header,
  .page-fullh.thread-active .inbox-filters { display: none; }

  /* --- 6. iOS zoom fix: 16px font on focusable inputs stops auto-zoom --- */
  input, textarea, select, .input, .composer-textarea { font-size: 16px; }

  /* --- 7. Comfortable touch targets --- */
  .nav-item { min-height: 40px; padding: 9px 12px; }
  .btn { min-height: 40px; padding: 8px 14px; }

  /* --- 8. Inbox filter chips: one swipeable row instead of a dense, wrapped
     block of 11+ chips that ate a third of the screen ("всё в кучке"). --- */
  .inbox-filters {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;          /* hide the scrollbar; swipe to scroll */
    padding-bottom: 6px;
  }
  .inbox-filters::-webkit-scrollbar { display: none; }
  .inbox-filters > * { flex: 0 0 auto; }       /* keep chip size; don't squash */
  .inbox-filters > span { padding-top: 7px; padding-bottom: 7px; } /* bigger tap target */
  .inbox-archive-chip { margin-left: 0 !important; } /* sits inline in the scroll row */

  /* --- 9. Page headers: let the action buttons wrap below the title instead of
     cramming onto one line next to the fixed hamburger button. --- */
  .page-header { flex-wrap: wrap; row-gap: 8px; }
  .page-title { font-size: 15px; }

  /* --- 10. Cards a touch tighter so content isn't squeezed against the edges --- */
  .card { padding: 12px; }

  /* --- 11. Full-height pages (inbox / lists / people). 100vh on iOS includes the
     browser toolbar AND ignored the 52px hamburger inset, so the bottom (the inbox
     composer + info panel) fell below the visible viewport — "не вижу половину
     диалога". dvh tracks the ACTUAL visible height; subtract the top + bottom
     padding of .main (52 + 14) so the page fits exactly on screen. --- */
  .app { height: 100dvh; }
  .page-fullh { height: calc(100dvh - 66px) !important; }
}

@media (max-width: 480px) {
  /* Single-column stat cards on the smallest screens. */
  .grid-4, .grid-3 { grid-template-columns: 1fr; }
}
