:root {
  --msg-brand: #0B2559;
  --msg-brand-strong: #071A40;
  --msg-accent: #B56B45;
  --msg-bg: #F4F6F8;
  --msg-panel: #FFFFFF;
  --msg-panel-soft: #F8FAFC;
  --msg-line: #DDE3EA;
  --msg-ink: #1F2937;
  --msg-muted: #667085;
  --msg-muted-soft: #98A2B3;
  --msg-shadow: 0 12px 34px rgba(15, 23, 42, .08);
  --msg-safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { min-width: 320px; min-height: 100%; }
body.messages-page-shell {
  min-height: 100dvh;
  margin: 0;
  overflow: hidden;
  color: var(--msg-ink);
  background: var(--msg-bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, [contenteditable="true"] { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }

.msg-topbar {
  position: relative;
  z-index: 20;
  height: 58px;
  color: var(--msg-ink);
  background: var(--msg-panel);
  border-bottom: 1px solid var(--msg-line);
}
.msg-topbar-inner {
  display: flex;
  align-items: center;
  gap: 12px;
  width: min(1440px, 100%);
  height: 58px;
  margin: 0 auto;
  padding: 8px 18px;
}
.msg-back {
  display: inline-grid;
  flex: 0 0 auto;
  place-items: center;
  width: 36px;
  height: 36px;
  color: var(--msg-brand);
  border: 1px solid var(--msg-line);
  border-radius: 10px;
  text-decoration: none;
  font-size: 20px;
  line-height: 1;
}
.msg-back:hover, .msg-back:focus-visible { border-color: var(--msg-brand); background: #F1F5F9; }
.msg-brand { flex: 0 0 auto; color: var(--msg-brand); font-size: 16px; font-weight: 850; letter-spacing: -.035em; text-decoration: none; }
.msg-heading { display: flex; min-width: 0; flex: 1; flex-direction: column; gap: 1px; }
.msg-heading strong { font-size: 14px; font-weight: 850; }
.msg-heading span { overflow: hidden; color: var(--msg-muted); font-size: 11px; text-overflow: ellipsis; white-space: nowrap; }
.msg-nav { display: flex; align-items: center; gap: 5px; }
.msg-nav a { padding: 8px 10px; color: var(--msg-muted); border-radius: 8px; font-size: 12px; font-weight: 800; text-decoration: none; }
.msg-nav a:hover, .msg-nav a:focus-visible, .msg-nav a.is-current { color: var(--msg-brand); background: #EEF3F9; }

#ch-app {
  display: grid;
  grid-template-columns: 350px minmax(0, 1fr);
  width: min(1440px, 100%);
  height: calc(100dvh - 58px);
  min-height: 520px;
  margin: 0 auto;
  overflow: hidden;
  background: var(--msg-panel);
  box-shadow: var(--msg-shadow);
}
#ch-sidebar { display: flex; min-width: 0; flex-direction: column; border-right: 1px solid var(--msg-line); background: var(--msg-panel); }
.ch-sb-head { display: flex; align-items: center; gap: 8px; padding: 18px 18px 12px; }
.ch-sb-title { min-width: 0; flex: 1; color: var(--msg-ink); font-size: 18px; font-weight: 850; letter-spacing: -.035em; }
.ch-icon-btn, .ch-tool-btn, .ch-send-btn {
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  min-width: 36px;
  height: 36px;
  padding: 0 9px;
  color: var(--msg-muted);
  background: transparent;
  border: 1px solid var(--msg-line);
  border-radius: 10px;
}
.ch-icon-btn:hover, .ch-icon-btn:focus-visible, .ch-tool-btn:hover, .ch-tool-btn:focus-visible { color: var(--msg-brand); border-color: #B7C5D8; background: #F5F8FB; }
.ch-sb-search-wrap { padding: 0 18px 12px; }
.ch-sb-search-rel { position: relative; }
.ch-sb-search-ico { position: absolute; top: 50%; left: 11px; color: var(--msg-muted-soft); transform: translateY(-50%); pointer-events: none; }
.ch-sb-search { width: 100%; height: 40px; padding: 0 12px 0 34px; color: var(--msg-ink); background: var(--msg-panel-soft); border: 1px solid var(--msg-line); border-radius: 10px; outline: none; }
.ch-sb-search:focus { border-color: var(--msg-brand); box-shadow: 0 0 0 3px rgba(11,37,89,.1); }
.ch-tabs { display: flex; gap: 6px; padding: 0 18px 12px; border-bottom: 1px solid var(--msg-line); }
.ch-tab { padding: 7px 10px; color: var(--msg-muted); background: transparent; border: 0; border-radius: 8px; font-size: 11px; font-weight: 800; }
.ch-tab:hover, .ch-tab:focus-visible, .ch-tab.active { color: var(--msg-brand); background: #EEF3F9; }
.ch-conv-list { min-height: 0; flex: 1; overflow: auto; padding: 10px; }
.ch-conv-item { display: grid; grid-template-columns: 44px minmax(0,1fr) auto; align-items: center; gap: 10px; width: 100%; margin-bottom: 4px; padding: 10px; color: var(--msg-ink); text-align: left; background: transparent; border: 1px solid transparent; border-radius: 12px; }
.ch-conv-item:hover, .ch-conv-item:focus-visible { background: #F5F8FB; border-color: var(--msg-line); outline: none; }
.ch-conv-item.active { background: #EEF3F9; border-color: #C9D5E4; }
.ch-conv-item[hidden] { display: none; }
.ch-conv-av, .ch-header-av { display: grid; place-items: center; width: 44px; height: 44px; overflow: hidden; color: #fff; background: var(--msg-brand); border-radius: 50%; font-size: 13px; font-weight: 850; }
.ch-conv-av img, .ch-header-av img, .ch-msg-av img { width: 100%; height: 100%; object-fit: cover; }
.ch-conv-body { min-width: 0; }
.ch-conv-name, .ch-conv-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-conv-name { font-size: 13px; font-weight: 800; }
.ch-conv-preview { margin-top: 4px; color: var(--msg-muted); font-size: 11px; }
.ch-conv-meta { display: grid; justify-items: end; gap: 5px; color: var(--msg-muted-soft); font-size: 10px; }
.ch-conv-badge { min-width: 19px; padding: 3px 6px; color: #fff; background: var(--msg-accent); border-radius: 999px; text-align: center; font-size: 10px; font-weight: 800; }
.ch-list-state { padding: 26px 16px; color: var(--msg-muted); text-align: center; font-size: 13px; line-height: 1.5; }
.ch-list-state.is-error { color: #B42318; }

#ch-main { position: relative; min-width: 0; background: var(--msg-panel-soft); }
#ch-no-conv, #ch-conv-view { width: 100%; height: 100%; }
#ch-no-conv { display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 10px; padding: 28px; color: var(--msg-muted); text-align: center; }
#ch-no-conv[hidden], #ch-conv-view[hidden] { display: none; }
#ch-no-conv svg { color: var(--msg-brand); }
#ch-no-conv h3 { margin: 0; color: var(--msg-ink); font-size: 22px; letter-spacing: -.035em; }
#ch-no-conv p { max-width: 300px; margin: 0; font-size: 13px; line-height: 1.5; }
.ch-new-msg-btn { min-height: 42px; padding: 0 16px; color: #fff; background: var(--msg-brand); border: 0; border-radius: 10px; font-size: 12px; font-weight: 850; }
.ch-new-msg-btn:hover, .ch-new-msg-btn:focus-visible { background: var(--msg-brand-strong); }
#ch-conv-view { display: flex; flex-direction: column; background: var(--msg-panel); }
#ch-header { display: flex; align-items: center; gap: 11px; min-height: 70px; padding: 12px 18px; border-bottom: 1px solid var(--msg-line); background: var(--msg-panel); }
#ch-header .ch-icon-btn { margin-right: 2px; }
.ch-header-av { flex: 0 0 40px; width: 40px; height: 40px; }
.ch-header-info { min-width: 0; flex: 1; }
.ch-header-name { overflow: hidden; color: var(--msg-ink); font-size: 14px; font-weight: 850; text-overflow: ellipsis; white-space: nowrap; }
.ch-header-status { margin-top: 3px; color: var(--msg-muted); font-size: 11px; }
.ch-messages { min-height: 0; flex: 1; overflow: auto; padding: 22px clamp(16px, 4vw, 56px); background: var(--msg-panel-soft); }
.ch-empty { display: grid; place-items: center; min-height: 100%; color: var(--msg-muted); text-align: center; }
.ch-empty-icon { margin-bottom: 8px; font-size: 28px; }
.ch-date-sep { display: flex; align-items: center; gap: 10px; margin: 14px 0; color: var(--msg-muted-soft); font-size: 10px; font-weight: 750; }
.ch-date-sep::before, .ch-date-sep::after { height: 1px; flex: 1; background: var(--msg-line); content: ""; }
.ch-msg { display: flex; align-items: flex-end; gap: 8px; max-width: min(700px, 88%); margin: 8px 0; }
.ch-msg.mine { margin-left: auto; flex-direction: row-reverse; }
.ch-msg-av { display: grid; place-items: center; flex: 0 0 30px; width: 30px; height: 30px; overflow: hidden; color: #fff; background: var(--msg-brand); border-radius: 50%; font-size: 10px; font-weight: 800; }
.ch-msg-inner { display: flex; min-width: 0; flex-direction: column; align-items: flex-start; }
.ch-msg.mine .ch-msg-inner { align-items: flex-end; }
.ch-msg-bubble { max-width: 100%; padding: 10px 13px; color: var(--msg-ink); background: #fff; border: 1px solid var(--msg-line); border-radius: 14px 14px 14px 5px; box-shadow: 0 3px 10px rgba(15,23,42,.035); white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; line-height: 1.45; }
.ch-msg.mine .ch-msg-bubble { color: #fff; background: var(--msg-brand); border-color: var(--msg-brand); border-radius: 14px 14px 5px 14px; }
.ch-msg-meta { display: flex; gap: 6px; margin-top: 4px; color: var(--msg-muted-soft); font-size: 9px; }
.ch-msg.mine .ch-msg-meta { justify-content: flex-end; }
.ch-msg-receipt { color: var(--msg-brand); }
.ch-msg.mine .ch-msg-receipt { color: var(--msg-accent); }
.ch-msg-media { display: block; max-width: 260px; max-height: 220px; margin-top: 8px; border-radius: 9px; }
.ch-msg-link { color: var(--msg-brand); font-size: 12px; }
.ch-compose { display: flex; align-items: flex-end; gap: 7px; padding: 12px 18px calc(12px + var(--msg-safe-bottom)); border-top: 1px solid var(--msg-line); background: var(--msg-panel); }
.ch-compose-input { min-height: 40px; max-height: 140px; flex: 1; overflow: auto; padding: 10px 12px; color: var(--msg-ink); background: var(--msg-panel-soft); border: 1px solid var(--msg-line); border-radius: 10px; outline: none; line-height: 1.35; }
.ch-compose-input:focus { border-color: var(--msg-brand); box-shadow: 0 0 0 3px rgba(11,37,89,.1); }
.ch-compose-input:empty::before { color: var(--msg-muted-soft); content: attr(data-placeholder); pointer-events: none; }
.ch-send-btn { color: #fff; background: var(--msg-brand); border-color: var(--msg-brand); }
.ch-send-btn:hover, .ch-send-btn:focus-visible { background: var(--msg-brand-strong); }

#ch-new-modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; background: rgba(15,23,42,.42); }
#ch-new-modal[hidden] { display: none; }
.ch-modal-box { width: min(460px, 100%); max-height: min(600px, 90dvh); overflow: auto; padding: 18px; background: var(--msg-panel); border: 1px solid var(--msg-line); border-radius: 16px; box-shadow: 0 24px 70px rgba(15,23,42,.2); }
.ch-modal-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.ch-modal-title { flex: 1; color: var(--msg-ink); font-size: 16px; font-weight: 850; }
.ch-modal-close { width: 34px; height: 34px; color: var(--msg-muted); background: transparent; border: 1px solid var(--msg-line); border-radius: 9px; }
.ch-user-search { width: 100%; height: 42px; padding: 0 12px; color: var(--msg-ink); background: var(--msg-panel-soft); border: 1px solid var(--msg-line); border-radius: 10px; outline: none; }
.ch-user-search:focus { border-color: var(--msg-brand); box-shadow: 0 0 0 3px rgba(11,37,89,.1); }
.ch-user-results { display: grid; gap: 5px; margin-top: 12px; }
.ch-user-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px; color: var(--msg-ink); text-align: left; background: transparent; border: 1px solid transparent; border-radius: 10px; }
.ch-user-item:hover, .ch-user-item:focus-visible { background: #F5F8FB; border-color: var(--msg-line); outline: none; }
.ch-user-avatar { display: grid; place-items: center; width: 36px; height: 36px; overflow: hidden; color: #fff; background: var(--msg-brand); border-radius: 50%; font-size: 11px; font-weight: 800; }
.ch-user-avatar img { width: 100%; height: 100%; object-fit: cover; }
.ch-user-name { font-size: 13px; font-weight: 800; }
.ch-user-handle { margin-top: 2px; color: var(--msg-muted); font-size: 11px; }

@media (max-width: 760px) {
  body.messages-page-shell { overflow: hidden; }
  .msg-topbar-inner { gap: 8px; padding: 8px 12px; }
  .msg-brand { font-size: 15px; }
  .msg-heading span { display: none; }
  .msg-nav { gap: 2px; }
  .msg-nav a { padding: 7px 8px; font-size: 11px; }
  #ch-app { display: block; height: calc(100dvh - 58px); min-height: 0; }
  #ch-sidebar, #ch-main { width: 100%; height: 100%; }
  #ch-sidebar { border-right: 0; }
  #ch-main { display: none; }
  body.is-conversation #ch-sidebar { display: none; }
  body.is-conversation #ch-main { display: block; }
  .ch-sb-head { padding: 14px 14px 10px; }
  .ch-sb-search-wrap { padding-inline: 14px; }
  .ch-tabs { padding-inline: 14px; }
  .ch-conv-list { padding: 8px; }
  #ch-header { min-height: 62px; padding: 10px 12px; }
  .ch-messages { padding: 16px 12px; }
  .ch-msg { max-width: 92%; }
  .ch-compose { padding-inline: 10px; }
  .ch-compose .ch-tool-btn { display: none; }
  .ch-compose-input { min-height: 42px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
