.portal-chat { width: 100%; max-width: 1480px; margin-inline: auto; overflow-x: hidden; color: var(--text, #172033); }
.portal-chat .chat-heading { margin-bottom: 22px; }
.portal-chat .chat-heading p:last-child { max-width: 78ch; margin-bottom: 0; }
.chat-layout { display: grid; grid-template-columns: minmax(280px, 340px) minmax(0, 1fr); gap: 18px; align-items: stretch; }
.chat-controls { align-self: start; }
.chat-controls .field { margin-bottom: 20px; }
.chat-controls select, .chat-panel textarea { width: 100%; min-height: 44px; border: 1px solid var(--border, #dbe3ef); border-radius: 9px; padding: 10px 12px; color: var(--text, #172033); background: var(--surface, #fff); }
.chat-agent-meta { margin: 0 0 20px; }
.chat-agent-meta > div { display: grid; grid-template-columns: 100px minmax(0, 1fr); gap: 10px; padding: 10px 0; border-top: 1px solid var(--border, #dbe3ef); }
.chat-agent-meta dt { color: var(--muted, #5b667a); font-size: .75rem; font-weight: 700; }
.chat-agent-meta dd { min-width: 0; margin: 0; overflow-wrap: anywhere; font-size: .85rem; }
.chat-controls > .button { width: 100%; min-height: 44px; }
.chat-control-help { margin: 12px 0 0; font-size: .8rem; }
.chat-panel { display: grid; grid-template-rows: auto minmax(240px, 1fr) auto auto; min-width: 0; height: min(720px, calc(100vh - 190px)); min-height: 540px; padding: 0 !important; overflow: hidden; }
.chat-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px 20px; border-bottom: 1px solid var(--border, #dbe3ef); }
.chat-toolbar h3 { margin: 0; }
.chat-toolbar span { color: var(--muted, #5b667a); font-size: .75rem; }
.chat-toolbar .button { min-height: 44px; }
.chat-transcript { min-width: 0; margin: 0; padding: 20px; overflow-y: auto; overscroll-behavior: contain; list-style: none; background: color-mix(in srgb, var(--surface-muted, #f1f5f9) 55%, transparent); }
.chat-turn { width: min(82%, 760px); margin: 0 0 16px; padding: 13px 15px; border: 1px solid var(--border, #dbe3ef); border-radius: 13px; background: var(--surface, #fff); box-shadow: 0 3px 12px rgba(30,64,175,.05); }
.chat-turn.user { margin-left: auto; border-color: color-mix(in srgb, var(--primary, #1e40af) 26%, var(--border, #dbe3ef)); background: color-mix(in srgb, var(--primary, #1e40af) 5%, var(--surface, #fff)); }
.chat-turn.pending { color: var(--muted, #5b667a); font-weight: 650; }
.chat-turn.error { border-color: color-mix(in srgb, var(--danger, #b91c1c) 40%, var(--border, #dbe3ef)); }
.chat-turn.error > strong { color: var(--danger, #b91c1c); }
.chat-turn-header { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 7px; }
.chat-turn-header time { color: var(--muted, #5b667a); font-size: .72rem; }
.chat-message-text { margin: 0 0 10px; color: inherit; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.55; }
.chat-empty-state { display: grid; min-height: 100%; place-items: center; color: var(--muted, #5b667a); text-align: center; }
.chat-status { min-height: 0; padding-inline: 20px; color: var(--danger, #b91c1c); font-size: .85rem; }
.chat-status:not(:empty) { padding-block: 11px; border-top: 1px solid var(--border, #dbe3ef); background: color-mix(in srgb, var(--danger, #b91c1c) 5%, var(--surface, #fff)); }
#chat-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 12px; align-items: end; padding: 16px 20px 20px; border-top: 1px solid var(--border, #dbe3ef); background: var(--surface, #fff); }
#chat-form .field { gap: 6px; }
#chat-form textarea { resize: vertical; max-height: 180px; font-family: inherit; line-height: 1.45; }
#chat-send { min-width: 124px; min-height: 44px; }
.chat-helper { display: flex; justify-content: space-between; gap: 12px; color: var(--muted, #5b667a); }
.chat-trace, .chat-chart-data { margin-top: 12px; border-top: 1px solid var(--border, #dbe3ef); padding-top: 10px; }
.chat-trace summary, .chat-chart-data summary { min-height: 44px; padding-block: 11px; cursor: pointer; font-weight: 700; }
.chat-trace-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 8px 0; }
.chat-trace-list div { min-width: 0; padding: 8px; background: var(--surface-muted, #f1f5f9); border-radius: 7px; }
.chat-trace-list dt { color: var(--muted, #5b667a); font-size: .7rem; font-weight: 700; }
.chat-trace-list dd { margin: 3px 0 0; overflow-wrap: anywhere; font-size: .8rem; }
.chat-trace-steps { padding-left: 22px; font-size: .8rem; line-height: 1.6; }
.chat-external-state { margin: 8px 0 0; font-size: .8rem; font-weight: 700; }
.chat-chart { margin: 14px 0 0; }
.chat-chart img { display: block; width: 100%; height: auto; border: 1px solid var(--border, #dbe3ef); border-radius: 9px; }
.chat-chart figcaption { margin-top: 7px; color: var(--muted, #5b667a); font-size: .8rem; }
.chat-chart table { display: block; max-width: 100%; overflow-x: auto; }
.chat-chart th, .chat-chart td { padding: 8px 10px; text-align: left; }
.chat-dialog { width: min(500px, calc(100vw - 28px)); border: 0; border-radius: 14px; padding: 24px; color: var(--text, #172033); background: var(--surface, #fff); box-shadow: 0 24px 70px rgba(15,23,42,.28); }
.chat-dialog::backdrop { background: rgba(15,23,42,.58); }
.chat-dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 20px; }
.portal-chat :focus-visible { outline: 3px solid rgba(59,130,246,.5); outline-offset: 2px; }

@media (max-width: 900px) {
  .chat-layout { grid-template-columns: 1fr; }
  .chat-controls { display: grid; grid-template-columns: minmax(0, 1fr) minmax(240px, .8fr); gap: 0 20px; width: 100%; }
  .chat-controls .field { grid-column: 1; }
  .chat-agent-meta { grid-column: 1; }
  .chat-controls > .button, .chat-control-help { grid-column: 2; }
  .chat-panel { height: auto; min-height: 620px; max-height: min(780px, calc(100dvh - 80px)); }
}
@media (max-width: 480px) {
  .portal-chat { width: 100%; overflow-x: hidden; }
  .chat-controls { display: block; width: 100%; }
  .chat-controls select, .chat-controls .button, .chat-toolbar .button, #chat-send { width: 100%; }
  .chat-panel { display: block; min-height: 0; max-height: none; height: auto; overflow: visible; }
  .chat-toolbar { align-items: stretch; flex-direction: column; }
  .chat-transcript { min-height: 320px; max-height: 58dvh; padding: 14px; }
  .chat-turn { width: 94%; }
  #chat-form { grid-template-columns: 1fr; padding: 14px; }
  #chat-form textarea { min-height: 96px; }
  .chat-helper { align-items: flex-start; flex-direction: column; gap: 3px; }
  .chat-trace-list { grid-template-columns: 1fr; }
  .chat-dialog-actions { display: grid; grid-template-columns: 1fr; }
  .chat-dialog-actions .button { width: 100%; min-height: 44px; }
}
@media (pointer: coarse) {
  .portal-chat button, .portal-chat select, .chat-trace summary, .chat-chart-data summary { min-height: 44px; }
}
@media (prefers-reduced-motion: reduce) {
  .portal-chat *, .portal-chat *::before, .portal-chat *::after { animation: none !important; scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
@media (prefers-color-scheme: dark) {
  .chat-turn, #chat-form, .chat-dialog, .chat-controls select, .chat-panel textarea { color: var(--text, #eef4ff); background: var(--surface, #111c2e); }
  .chat-transcript { background: color-mix(in srgb, var(--surface-muted, #17243a) 65%, transparent); }
  .chat-turn.user { background: color-mix(in srgb, var(--primary, #60a5fa) 13%, var(--surface, #111c2e)); }
}
