:root {
  --ink: #172026;
  --muted: #6b7680;
  --line: #dfe4e8;
  --surface: #f7f8f7;
  --panel: #ffffff;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --gold: #d89a18;
  --danger: #a33b3b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--surface);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

.mobile-topbar,
.menu-scrim,
.sidebar-mobile-row {
  display: none;
}

.desktop-brand {
  display: flex;
}

.app-shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 22px;
  color: #ffffff;
  background: #172026;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #ffffff;
  text-decoration: none;
}

.brand small {
  display: block;
  color: #b8c1c8;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  color: #10201e;
  background: #9be7d8;
  font-weight: 800;
}

.nav-list {
  display: grid;
  gap: 4px;
}

.nav-list a {
  padding: 10px 12px;
  border-radius: 8px;
  color: #dbe4e8;
  text-decoration: none;
}

.nav-list a.active,
.nav-list a:hover {
  background: #24323a;
  color: #ffffff;
}

.sidebar-footer {
  margin-top: auto;
}

.sidebar-close,
.mobile-menu-button {
  border: 0;
  background: transparent;
}

.content {
  padding: 28px;
  min-width: 0;
}

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 24px;
}

.page-head h1 {
  margin: 0;
  font-size: 32px;
  line-height: 1.15;
}

.page-head p,
.muted,
.empty {
  color: var(--muted);
}

.head-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 22px;
}

.metric,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.metric {
  padding: 16px;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 28px;
}

.workspace-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.8fr);
  gap: 18px;
}

.panel {
  padding: 18px;
}

.panel h2 {
  margin: 0 0 14px;
  font-size: 18px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.list-table {
  display: grid;
  gap: 2px;
}

.list-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
}

.list-row span:nth-child(2) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list-row time,
.telegram-id,
.note time {
  color: var(--muted);
  font-size: 12px;
}

.filters {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) 180px 180px auto;
  gap: 10px;
  margin-bottom: 16px;
}

.strong-link {
  color: var(--accent-strong);
  font-weight: 700;
  text-decoration: none;
}

.status {
  display: inline-block;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.status-active {
  background: #d7f5ed;
  color: #07584f;
}

.status-archived {
  background: #eef0f2;
  color: #4d5963;
}

.status-blocked {
  background: #f7dddd;
  color: var(--danger);
}

.conversation-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 18px;
}

.chat-app {
  position: relative;
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  height: calc(100svh - 56px);
  min-height: 640px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel);
}

.chat-contacts {
  min-width: 0;
  border-right: 1px solid var(--line);
  background: #fbfcfb;
}

.chat-contacts-head,
.chat-pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 76px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.chat-icon-button {
  display: inline-grid;
  place-content: center;
  gap: 4px;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.chat-icon-button span,
.mobile-menu-button span {
  display: block;
  width: 18px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
}

.chat-contacts-head h1,
.chat-pane-head h2 {
  margin: 0;
  font-size: 22px;
}

.chat-contacts-head p,
.chat-pane-head p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.chat-contact-list {
  height: calc(100% - 76px);
  overflow: auto;
}

.chat-contact {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  gap: 10px;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  background: transparent;
  text-align: left;
}

.chat-contact:hover,
.chat-contact.active {
  background: #eaf7f4;
}

.avatar {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  border-radius: 50%;
  color: #083b36;
  background: #bfece4;
  font-weight: 800;
}

.contact-copy {
  min-width: 0;
}

.contact-copy strong,
.contact-copy small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contact-copy small,
.contact-meta {
  color: var(--muted);
  font-size: 12px;
}

.contact-meta {
  display: grid;
  gap: 4px;
  justify-items: end;
}

.contact-meta b {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  border-radius: 999px;
  color: #fff;
  background: var(--accent);
  font-size: 12px;
}

.chat-pane {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  overflow: hidden;
  background: #f3f6f5;
}

.chat-title {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.chat-title > div {
  min-width: 0;
}

.chat-title h2,
.chat-title p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-detail-link {
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
}

.socket-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.socket-status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #a8b2b8;
}

.socket-status.connected {
  color: var(--accent-strong);
}

.socket-status.connected::before {
  background: var(--accent);
}

.chat-alert {
  margin: 10px 16px 0;
}

.chat-alert-zone:empty {
  display: block;
  min-height: 0;
}

.chat-message-stream {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 18px;
  overflow: auto;
}

.chat-bubble {
  max-width: min(680px, 82%);
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.chat-bubble.outgoing {
  align-self: flex-end;
  border-color: #b9dfd8;
  background: #e6f8f4;
}

.chat-bubble p {
  margin: 0 0 8px;
}

.chat-composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: end;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: #ffffff;
}

.chat-composer textarea {
  min-height: 42px;
  max-height: 120px;
  resize: none;
}

.chat-attach-button {
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #ffffff;
  font-weight: 700;
}

.chat-send-drawer {
  display: none;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  max-height: 38svh;
  padding: 12px;
  overflow: auto;
  border-top: 1px solid var(--line);
  background: #ffffff;
}

.chat-app.attachments-open .chat-send-drawer {
  display: grid;
}

.drawer-head {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.media-tool {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.media-tool h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.chat-send-drawer form {
  display: grid;
  gap: 8px;
}

.mobile-contact-toggle {
  display: none;
}

.empty-chat {
  display: grid;
  place-content: center;
  padding: 40px;
  text-align: center;
}

.message-panel {
  min-height: 70vh;
}

.message-stream {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  max-width: min(720px, 88%);
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
}

.message.outgoing {
  align-self: flex-end;
  border-color: #b9dfd8;
  background: #effbf8;
}

.message-meta {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
}

.message p {
  margin: 0 0 8px;
}

.media-preview {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 8px;
  object-fit: contain;
  background: #111;
}

.side-stack {
  display: grid;
  gap: 14px;
  align-content: start;
}

.notice {
  padding: 10px;
  border-radius: 8px;
  color: #654706;
  background: #fff3cd;
  font-size: 13px;
}

.segmented {
  display: flex;
  gap: 6px;
}

.note {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}

.note time {
  display: block;
  margin-top: 4px;
}

.settings-list {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr);
  gap: 10px;
  margin: 0;
}

.settings-list dt {
  color: var(--muted);
  font-weight: 600;
}

.settings-list dd {
  margin: 0;
  min-width: 0;
  overflow-wrap: anywhere;
}

.narrow-panel {
  max-width: 620px;
  margin-top: 18px;
}

.raw-update {
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}

pre {
  max-width: 100%;
  overflow: auto;
  padding: 12px;
  border-radius: 8px;
  background: #101820;
  color: #d6f2ee;
}

.btn-primary {
  --bs-btn-bg: var(--accent);
  --bs-btn-border-color: var(--accent);
  --bs-btn-hover-bg: var(--accent-strong);
  --bs-btn-hover-border-color: var(--accent-strong);
}

.btn-warning {
  --bs-btn-bg: var(--gold);
  --bs-btn-border-color: var(--gold);
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: #172026;
}

.login-panel {
  width: min(420px, 100%);
  padding: 28px;
  border-radius: 8px;
  background: #ffffff;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.login-brand h1 {
  margin: 0;
  font-size: 24px;
}

.login-brand p {
  margin: 2px 0 0;
  color: var(--muted);
}

@media (max-width: 1120px) {
  .metric-grid {
    grid-template-columns: repeat(3, minmax(130px, 1fr));
  }

  .workspace-grid,
  .conversation-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 760px) {
  .mobile-topbar {
    position: sticky;
    top: 0;
    z-index: 80;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
    padding: 0 12px;
    border-bottom: 1px solid var(--line);
    background: #ffffff;
  }

  .mobile-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    color: var(--ink);
    text-decoration: none;
  }

  .mobile-brand .brand-mark {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    font-size: 13px;
  }

  .mobile-brand strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
  }

  .mobile-menu-button {
    display: inline-grid;
    place-content: center;
    gap: 4px;
    width: 42px;
    height: 42px;
    border-radius: 8px;
    color: var(--ink);
  }

  .menu-scrim {
    position: fixed;
    inset: 0;
    z-index: 90;
    background: rgba(10, 18, 22, 0.48);
  }

  body.menu-open .menu-scrim {
    display: block;
  }

  .app-shell {
    display: block;
    min-height: calc(100svh - 56px);
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 100;
    width: min(84vw, 318px);
    height: 100svh;
    overflow: auto;
    transform: translateX(-104%);
    transition: transform 180ms ease;
    box-shadow: 20px 0 60px rgba(8, 16, 20, 0.24);
  }

  body.menu-open .sidebar {
    transform: translateX(0);
  }

  .desktop-brand {
    display: none;
  }

  .sidebar-mobile-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
  }

  .sidebar-close {
    color: #ffffff;
    font-weight: 700;
  }

  .content {
    padding: 16px;
  }

  .page-head,
  .head-actions {
    display: grid;
  }

  .metric-grid,
  .filters {
    grid-template-columns: 1fr;
  }

  .message {
    max-width: 100%;
  }

  .chat-app {
    grid-template-columns: 1fr;
    height: calc(100svh - 56px);
    min-height: 0;
    margin: -16px;
    border: 0;
    border-radius: 0;
  }

  .chat-contacts {
    display: none;
    border-right: 0;
  }

  .chat-app.show-contacts .chat-contacts {
    display: block;
  }

  .chat-app.show-contacts .chat-pane {
    display: none;
  }

  .mobile-contact-toggle {
    display: inline-grid;
  }

  .chat-pane-head {
    min-height: 62px;
    padding: 8px 10px;
    gap: 8px;
  }

  .chat-title {
    gap: 8px;
    flex: 1 1 auto;
  }

  .chat-title .avatar {
    width: 36px;
    height: 36px;
    font-size: 14px;
  }

  .chat-pane-head h2 {
    font-size: 16px;
  }

  .chat-pane-head p {
    font-size: 11px;
  }

  .socket-status {
    font-size: 0;
    gap: 0;
  }

  .socket-status::before {
    width: 10px;
    height: 10px;
  }

  .chat-detail-link {
    display: none;
  }

  .chat-message-stream {
    gap: 8px;
    padding: 10px;
  }

  .chat-bubble {
    max-width: 95%;
    padding: 9px 10px;
  }

  .message-meta {
    gap: 8px;
    font-size: 11px;
  }

  .chat-composer {
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 6px;
    padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .chat-attach-button,
  .chat-composer .btn {
    min-height: 40px;
    padding: 0 10px;
    font-size: 13px;
  }

  .chat-composer textarea {
    min-height: 40px;
    font-size: 16px;
  }

  .chat-send-drawer {
    position: absolute;
    right: 0;
    bottom: 57px;
    left: 0;
    z-index: 12;
    grid-template-columns: 1fr;
    max-height: 46svh;
    padding: 10px max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    box-shadow: 0 -18px 44px rgba(17, 32, 38, 0.18);
  }

  .media-tool {
    padding: 10px;
  }

  .chat-contacts-head {
    min-height: 62px;
  }

  .chat-contact-list {
    height: calc(100% - 62px);
  }

  .chat-contact {
    padding: 11px 12px;
  }
}
