.content:has(.chat-page) {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 14px 16px;
}

.chat-page {
  width: 100%;
  min-width: 0;
  height: calc(100dvh - var(--topbar-height) - 28px);
  min-height: 640px;
}

.chat-shell {
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.chat-list-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--surface-soft);
}

.chat-list-header {
  display: grid;
  gap: 10px;
  padding: 14px;
  flex: 0 0 auto;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 7px;
}

.chat-search-input {
  width: 100%;
  min-width: 0;
}

.chat-filters {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding: 1px 0 3px;
  scrollbar-width: thin;
}

.chat-filter {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 7px 10px;
  background: var(--surface-soft);
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
  cursor: pointer;
}

.chat-filter.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
}

.conversation-list {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.conversation-item {
  width: 100%;
  display: flex;
  gap: 11px;
  padding: 13px 14px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.conversation-item:hover,
.conversation-item.active {
  background: var(--surface);
}

.conversation-item.active {
  box-shadow: inset 4px 0 0 var(--primary);
}

.conversation-meta,
.chat-top-main {
  flex: 1;
  min-width: 0;
}

.conversation-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.conversation-name {
  overflow: hidden;
  font-weight: 850;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-time {
  flex: 0 0 auto;
  color: var(--text-muted);
  font-size: 11px;
}

.conversation-preview {
  margin-top: 4px;
  overflow: hidden;
  color: var(--text-soft);
  font-size: 12.5px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-tags {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.unread-pill {
  min-width: 22px;
  height: 22px;
  display: inline-grid;
  place-items: center;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  font-size: 11px;
  font-weight: 900;
}

.chat-workspace {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 300px);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top top"
    "timeline timeline"
    "messages tools"
    "composer composer";
  overflow: hidden;
  background: var(--bg);
}

.chat-top {
  grid-area: top;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-top-main > strong {
  display: block;
  font-size: 15.5px;
}

.chat-top-main > span {
  display: block;
  margin-top: 3px;
  overflow: hidden;
  color: var(--text-soft);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-top-badges,
.chat-actions {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
}

.chat-top-badges {
  margin-top: 7px;
}

.chat-actions {
  justify-content: flex-end;
}

.chat-actions .soft-button[data-chat-status="escalated"] {
  border-color: color-mix(in srgb, var(--red) 50%, var(--border));
  background: var(--red-bg);
  color: var(--red);
}

.chat-more-menu {
  position: relative;
}

.chat-more-menu > summary {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 800;
  list-style: none;
  cursor: pointer;
}

.chat-more-menu > summary::-webkit-details-marker { display: none; }
.chat-more-menu > div {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 15;
  min-width: 180px;
  display: grid;
  gap: 7px;
  padding: 9px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow-md);
}

.mobile-back-btn,
.mobile-chat-context {
  display: none;
}

.order-timeline-strip {
  grid-area: timeline;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 16px;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
  background: var(--surface-soft);
}

.timeline-step {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.step-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--border-strong);
}

.timeline-step.done { color: var(--green); }
.timeline-step.done .step-dot { background: var(--green); }
.timeline-step.active { color: var(--primary-hover); }
.timeline-step.active .step-dot { background: var(--primary); box-shadow: 0 0 0 4px var(--primary-soft); }

.message-area {
  grid-area: messages;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px;
  background:
    radial-gradient(circle at top left, rgba(245,124,0,.08), transparent 340px),
    linear-gradient(180deg, rgba(38,50,56,.035), transparent 260px),
    var(--bg);
}

.message {
  width: fit-content;
  max-width: min(78%, 740px);
  display: grid;
  gap: 5px;
}

.message.in,
.message.aglo { align-self: flex-start; }
.message.out { align-self: flex-end; }
.message.internal { align-self: center; max-width: min(90%, 820px); }

.message-bubble {
  max-width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  line-height: 1.45;
  box-shadow: var(--shadow-sm);
}

.message.in .message-bubble { border-bottom-left-radius: 5px; }
.message.out .message-bubble { border-color: var(--primary); border-bottom-right-radius: 5px; background: var(--primary); color: #fff; }
.message.aglo .message-bubble { border-color: color-mix(in srgb, var(--blue) 25%, transparent); background: var(--blue-bg); color: var(--blue); }
.message.internal .message-bubble { border: 1px dashed color-mix(in srgb, var(--amber) 55%, var(--border)); border-radius: 14px; background: var(--amber-bg); font-size: 12.5px; text-align: center; }

.message-author { margin-bottom: 4px; font-size: 11.5px; font-weight: 900; opacity: .78; }
.message-text { overflow-wrap: anywhere; white-space: pre-wrap; line-height: 1.55; }
.message-meta { display: flex; gap: 8px; padding-inline: 4px; color: var(--text-muted); font-size: 11px; }
.message.out .message-meta { justify-content: flex-end; }

.media-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  font-size: 12px;
  font-weight: 800;
}
.media-chip svg { width: 16px; height: 16px; }

.message-actions-row {
  display: flex;
  justify-content: flex-end;
  gap: 7px;
  flex-wrap: wrap;
  margin-top: 9px;
}

.tiny-action {
  min-height: 28px;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--primary-hover);
  font-size: 11px;
  font-weight: 850;
  cursor: pointer;
}
.tiny-action:hover { border-color: var(--primary); background: var(--primary-soft); }
.tiny-action.danger { color: var(--red); }

.chat-side-tools {
  grid-area: tools;
  min-width: 0;
  min-height: 0;
  display: grid;
  align-content: start;
  gap: 12px;
  overflow-y: auto;
  padding: 14px;
  border-left: 1px solid var(--border);
  background: var(--surface-soft);
}

.chat-tool-card {
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.chat-tool-card.primary-tool { border-color: color-mix(in srgb, var(--primary) 24%, var(--border)); }
.chat-tool-card h3 { margin: 0 0 8px; font-size: 14px; }
.chat-tool-card p { margin: 0 0 12px; color: var(--text-soft); font-size: 12.5px; line-height: 1.4; }

.quick-actions-grid,
.quick-link-grid,
.template-quick-list {
  display: grid;
  gap: 8px;
}
.quick-actions-grid .soft-button { justify-content: flex-start; min-height: 36px; border-radius: 12px; }

.quick-link-card,
.template-quick-button {
  width: 100%;
  min-width: 0;
  display: grid;
  gap: 3px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-soft);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.quick-link-card:hover,
.template-quick-button:hover { border-color: var(--primary); background: var(--primary-soft); }
.quick-link-card span,
.template-quick-button span { overflow-wrap: anywhere; color: var(--text-soft); font-size: 12px; }
.template-quick-list { max-height: 240px; overflow-y: auto; }
.soft-button.full { width: 100%; margin-top: 10px; justify-content: center; }

.chat-contact-card {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-soft);
}
.chat-contact-card strong,
.chat-contact-card span { display: block; overflow-wrap: anywhere; }
.chat-contact-card span { margin-top: 3px; color: var(--text-soft); font-size: 12px; }

.chat-order-card {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-soft);
}
.chat-order-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.chat-order-card p { margin: 0 0 10px; color: var(--text-soft); line-height: 1.42; }
.chat-order-grid { display: grid; gap: 8px; margin-top: 8px; }
.chat-order-grid.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.mini-field {
  min-width: 0;
  display: grid;
  gap: 2px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.mini-field span { color: var(--text-muted); font-size: 10.5px; font-weight: 900; letter-spacing: .04em; text-transform: uppercase; }
.mini-field strong { overflow-wrap: anywhere; font-size: 12.5px; line-height: 1.25; }
.chat-order-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.chat-info-empty { padding: 14px; border: 1px dashed var(--border-strong); border-radius: 14px; background: var(--surface-soft); color: var(--text-soft); line-height: 1.45; }

.composer {
  grid-area: composer;
  z-index: 4;
  flex: 0 0 auto;
  padding: 11px 14px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer-tools { display: flex; gap: 7px; flex-wrap: wrap; margin-bottom: 9px; }
.composer-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: 9px; }
.composer-row textarea { min-height: 44px; max-height: 110px; resize: vertical; padding: 12px 14px; border-radius: 20px; }
.send-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 18px;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  font-weight: 900;
}
.send-button svg { width: 18px; height: 18px; }

@media (max-width: 1320px) and (min-width: 981px) {
  .chat-shell { grid-template-columns: minmax(260px, 310px) minmax(0, 1fr); }
  .chat-workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto auto;
    grid-template-areas: "top" "timeline" "messages" "tools" "composer";
  }
  .chat-side-tools {
    max-height: 210px;
    display: flex;
    gap: 10px;
    overflow-x: auto;
    overflow-y: hidden;
    border-top: 1px solid var(--border);
    border-left: 0;
  }
  .chat-tool-card { flex: 0 0 275px; overflow-y: auto; }
  .chat-actions { width: 100%; justify-content: flex-start; }
  .chat-top { align-items: flex-start; flex-wrap: wrap; }
}

@media (max-width: 980px) {
  .content:has(.chat-page) { padding: 0; }
  .chat-page { height: calc(100dvh - var(--topbar-height)); min-height: 0; }
  .chat-shell {
    grid-template-columns: 1fr;
    height: 100%;
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }
  .chat-list-panel { display: flex !important; border-right: 0; }
  .chat-workspace { display: none !important; }
  .chat-shell.mobile-chat-open .chat-list-panel { display: none !important; }
  .chat-shell.mobile-chat-open .chat-workspace {
    display: grid !important;
    height: 100%;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "top" "messages" "composer";
  }
  .mobile-back-btn {
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    display: inline-grid;
    place-items: center;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
  }
  .order-timeline-strip,
  .chat-side-tools { display: none !important; }
  .chat-top { align-items: flex-start; flex-wrap: wrap; padding: 10px 12px; }
  .chat-actions { width: 100%; justify-content: flex-start; }
  .mobile-chat-context { width: 100%; display: block; }
  .mobile-chat-context > summary {
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-soft);
    color: var(--text-soft);
    font-size: 12px;
    font-weight: 800;
    cursor: pointer;
  }
  .mobile-chat-context-body { display: grid; gap: 10px; max-height: 42dvh; overflow-y: auto; padding-top: 10px; }
  .message-area { padding: 13px; }
  .message { max-width: 90%; }
  .composer { position: sticky; bottom: 0; padding: 9px 10px; }
  .composer-tools { overflow-x: auto; flex-wrap: nowrap; }
}

@media (max-width: 620px) {
  .chat-list-header { padding: 11px; }
  .chat-search-row { grid-template-columns: minmax(0, 1fr) auto; }
  .chat-search-row .primary-soft { grid-column: 1 / -1; width: 100%; }
  .conversation-item { padding: 11px; }
  .chat-top-main > span { white-space: normal; }
  .chat-actions { overflow-x: auto; flex-wrap: nowrap; padding-bottom: 2px; }
  .chat-actions > * { flex: 0 0 auto; }
  .chat-more-menu > div { position: fixed; right: 12px; left: 12px; top: auto; bottom: 82px; }
  .message { max-width: 96%; }
  .message-bubble { font-size: 13.5px; }
  .message-actions-row { justify-content: stretch; }
  .message-actions-row .tiny-action { flex: 1 1 100%; }
  .chat-order-grid.compact { grid-template-columns: 1fr; }
  .composer-row { grid-template-columns: minmax(0, 1fr) auto; }
  .send-button { min-width: 44px; padding: 0 13px; }
  .send-button span { display: none; }
}
