/* fp-local-lab broker chat UI — Foundry Factory v0.2.0 */
:root {
  --bg: #0f1419;
  --bg-elev: #1a222c;
  --bg-chat: #121820;
  --border: #2a3544;
  --text: #e8eef4;
  --muted: #8b9aab;
  --accent: #3d9cf0;
  --accent-dim: #1e4d7a;
  --user: #243044;
  --agent: #1a2836;
  --warn: #e6b84d;
  --danger: #e06c75;
  --ok: #6bcb8d;
  --radius: 12px;
  --font: "IBM Plex Sans", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden; /* chat owns scroll; page does not jump away */
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

.shell {
  /* Full viewport shell — chat stays on screen with internal scroll */
  height: 100dvh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: none;
  width: 100%;
  margin: 0 auto;
  padding: 0 1rem;
  box-sizing: border-box;
}

.top {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.1rem 0 0.85rem;
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 0.85rem; }
.mark {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 8px;
  background:
    linear-gradient(135deg, var(--accent) 0%, #6b5cff 55%, #2a8f6f 100%);
  box-shadow: 0 0 0 1px rgba(255,255,255,0.08), 0 8px 24px rgba(61,156,240,0.25);
}
.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.sub { margin: 0.15rem 0 0; color: var(--muted); font-size: 0.85rem; }

.top-actions { display: flex; align-items: center; gap: 0.5rem; }

/* Access / auth banners (sign-in, 401, 403, wrong tenant) */
.access-banner {
  margin-top: 0.85rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elev);
  overflow: hidden;
}
.access-banner[data-kind="info"] {
  border-color: rgba(61, 156, 240, 0.4);
  background: linear-gradient(180deg, rgba(61, 156, 240, 0.12), var(--bg-elev));
}
.access-banner[data-kind="warn"] {
  border-color: rgba(230, 184, 77, 0.45);
  background: linear-gradient(180deg, rgba(230, 184, 77, 0.12), var(--bg-elev));
}
.access-banner[data-kind="danger"] {
  border-color: rgba(224, 108, 117, 0.5);
  background: linear-gradient(180deg, rgba(224, 108, 117, 0.12), var(--bg-elev));
}
.access-banner[data-kind="ok"] {
  border-color: rgba(107, 203, 141, 0.4);
  background: linear-gradient(180deg, rgba(107, 203, 141, 0.1), var(--bg-elev));
}
.access-banner-inner {
  display: flex;
  align-items: flex-start;
  gap: 0.85rem;
  padding: 0.85rem 1rem;
}
.access-banner-text { flex: 1; min-width: 0; }
.access-banner-text strong {
  display: block;
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}
.access-banner-text p {
  margin: 0;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.4;
}
.access-banner-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
}
.access-banner-dismiss {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.15rem 0.35rem;
  border-radius: 6px;
}
.access-banner-dismiss:hover { color: var(--text); background: rgba(255,255,255,0.06); }

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2.5rem 1.5rem;
  color: var(--muted);
  gap: 0.55rem;
}
.empty-state[hidden] { display: none !important; }
.empty-visual {
  position: relative;
  width: 3.5rem;
  height: 3.5rem;
  margin-bottom: 0.35rem;
  display: grid;
  place-items: center;
}
.empty-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 10%, transparent);
}
.empty-state .empty-icon {
  font-size: 1.5rem;
  color: var(--accent);
  opacity: 0.95;
  line-height: 1;
}
.empty-state h2 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.01em;
}
.empty-state p {
  margin: 0;
  max-width: 26rem;
  font-size: 0.9rem;
  line-height: 1.5;
}
.empty-hints {
  list-style: none;
  margin: 0.15rem 0 0.85rem;
  padding: 0;
  max-width: 26rem;
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--muted);
}
.empty-hints li {
  margin: 0.2rem 0;
  padding: 0.35rem 0.65rem;
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel, #1a2332) 80%, transparent);
  border: 1px solid color-mix(in srgb, var(--border, #2d3a4d) 80%, transparent);
}
.empty-state #empty-sign-in {
  min-width: 12rem;
}
@media (prefers-reduced-motion: no-preference) {
  .empty-ring {
    animation: empty-pulse 2.8s ease-in-out infinite;
  }
  @keyframes empty-pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.06); opacity: 0.75; }
  }
}
.chat.locked .composer {
  opacity: 0.55;
  pointer-events: none;
}
.chat.locked .transcript:empty + .composer,
.chat:has(.empty-state:not([hidden])) .transcript:empty {
  min-height: 0;
}

.pill {
  font-family: var(--mono);
  font-size: 0.72rem;
  padding: 0.3rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--muted);
  white-space: nowrap;
}
.pill.ok { color: var(--ok); border-color: rgba(107,203,141,0.35); }
.pill.warn { color: var(--warn); border-color: rgba(230,184,77,0.35); }
.pill.danger { color: var(--danger); border-color: rgba(224,108,117,0.35); }

.rail {
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
}

.main {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: minmax(220px, 280px) 1fr;
  gap: 1rem;
  padding-top: 0.75rem;
  padding-bottom: 0.5rem;
  min-height: 0; /* critical: allow .chat/.transcript to shrink + scroll */
}

@media (max-width: 800px) {
  .main {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .rail { order: 2; max-height: 28vh; overflow-y: auto; }
  .chat { order: 1; min-height: 0; }
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  margin-bottom: 0.75rem;
}
.card.muted { opacity: 0.92; }
.card h2 {
  margin: 0 0 0.65rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 600;
}

.meta { margin: 0; }
.meta > div {
  display: grid;
  grid-template-columns: 5.5rem 1fr;
  gap: 0.35rem;
  padding: 0.28rem 0;
  border-bottom: 1px solid rgba(42,53,68,0.6);
  font-size: 0.82rem;
}
.meta > div:last-child { border-bottom: 0; }
.meta dt { color: var(--muted); margin: 0; }
.meta dd {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.75rem;
  word-break: break-all;
}

.hint { margin: 0 0 0.6rem; font-size: 0.78rem; color: var(--warn); }
label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.55rem;
}
input, textarea {
  width: 100%;
  margin-top: 0.25rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.88rem;
  padding: 0.45rem 0.55rem;
}
input:focus, textarea:focus {
  outline: 2px solid var(--accent-dim);
  border-color: var(--accent);
}

.bullets {
  margin: 0;
  padding-left: 1.1rem;
  color: var(--muted);
  font-size: 0.82rem;
}
.bullets li { margin-bottom: 0.4rem; }
.bullets strong { color: var(--text); font-weight: 500; }

.thread-filter {
  width: 100%;
  box-sizing: border-box;
  margin: 0 0 0.5rem;
  padding: 0.45rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
}
.thread-filter:focus {
  outline: none;
  border-color: var(--accent);
}
.thread-filter::placeholder {
  color: var(--muted);
}

.thread-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 28vh;
  overflow-y: auto;
}
.thread-list button.thread-rename {
  flex: 0 0 auto;
  width: 2rem;
  min-width: 2rem;
  padding: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1;
}
.thread-list button.thread-rename:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.thread-list li {
  margin: 0 0 0.35rem;
}
.thread-list li.thread-row {
  display: flex;
  align-items: stretch;
  gap: 0.25rem;
}
.thread-list button.thread-item {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 0.45rem 0.55rem;
  cursor: pointer;
  font-size: 0.82rem;
}
.thread-list button.thread-item:hover { border-color: var(--accent); }
.thread-list button.thread-item.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
}
.thread-list .thread-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread-list .thread-meta {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  margin-top: 0.15rem;
}
.thread-list button.thread-pin {
  flex: 0 0 auto;
  width: 2rem;
  min-width: 2rem;
  padding: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1;
}
.thread-list button.thread-pin:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.thread-list button.thread-pin.is-pinned {
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 45%, var(--border));
}
.thread-share {
  margin-top: 0.75rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}
.thread-share h3 {
  margin: 0 0 0.4rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.thread-share-actions {
  display: flex;
  gap: 0.35rem;
  margin: 0.4rem 0;
  flex-wrap: wrap;
}
.share-list {
  list-style: none;
  margin: 0.35rem 0 0;
  padding: 0;
  font-size: 0.78rem;
  color: var(--muted);
}
.share-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.35rem;
  align-items: center;
  margin-bottom: 0.25rem;
}
.share-list button {
  font-size: 0.72rem;
  padding: 0.2rem 0.4rem;
}

.chat {
  display: flex;
  flex-direction: column;
  background: var(--bg-chat);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 0;
  height: 100%;
  max-height: 100%;
  overflow: hidden; /* transcript scrolls inside; composer stays pinned */
}

.transcript {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-gutter: stable;
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  /* Visible scrollbar so long threads are obvious */
  scrollbar-width: thin;
  scrollbar-color: var(--accent-dim) var(--bg-elev);
}
.transcript::-webkit-scrollbar {
  width: 10px;
}
.transcript::-webkit-scrollbar-track {
  background: var(--bg-elev);
  border-radius: 8px;
}
.transcript::-webkit-scrollbar-thumb {
  background: var(--accent-dim);
  border-radius: 8px;
  border: 2px solid var(--bg-elev);
}
.transcript::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}

.msg {
  max-width: min(92%, 40rem);
  padding: 0.7rem 0.9rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user {
  align-self: flex-end;
  background: var(--user);
  border-bottom-right-radius: 4px;
}
.msg.agent {
  align-self: flex-start;
  background: var(--agent);
  border-bottom-left-radius: 4px;
}
.msg.system {
  align-self: center;
  background: transparent;
  border-style: dashed;
  color: var(--muted);
  font-size: 0.82rem;
  max-width: 95%;
}
.msg .who {
  display: block;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 0.3rem;
  font-weight: 600;
}
.msg .notice {
  margin-top: 0.55rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  color: var(--warn);
  font-size: 0.82rem;
}
.msg.error { border-color: rgba(224,108,117,0.5); color: #f0b4b8; }

.msg.thinking {
  opacity: 0.92;
  border-style: dashed;
  border-color: var(--accent-dim);
}
.msg.streaming {
  border-style: solid;
  opacity: 1;
}
.msg .stream-body {
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.thinking .think-row {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--muted);
  font-size: 0.9rem;
}
/* HTML hidden must win over display:flex above — else "Writing 36s" sticks over answer */
.msg.thinking .think-row[hidden],
.msg .think-row[hidden] {
  display: none !important;
}
.msg.thinking .dots {
  display: inline-flex;
  gap: 0.22rem;
  align-items: center;
}
.msg.thinking .dots span {
  width: 0.35rem;
  height: 0.35rem;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.35;
  animation: think-bounce 1.2s ease-in-out infinite;
}
.msg.thinking .dots span:nth-child(2) { animation-delay: 0.15s; }
.msg.thinking .dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes think-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.3; }
  40% { transform: translateY(-3px); opacity: 1; }
}
.msg.thinking .elapsed {
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  margin-left: auto;
}

.composer {
  flex: 0 0 auto; /* always visible at bottom of chat pane */
  border-top: 1px solid var(--border);
  padding: 0.75rem 0.9rem 0.9rem;
  background: var(--bg-elev);
}
.composer textarea {
  resize: vertical;
  min-height: 2.6rem;
  max-height: 8rem;
}
.composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.55rem;
  gap: 0.75rem;
}
.status { font-size: 0.78rem; color: var(--muted); min-height: 1.2em; }

.btn {
  font-family: var(--font);
  font-size: 0.88rem;
  font-weight: 500;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0.45rem 0.9rem;
  cursor: pointer;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(180deg, #4aa6f5 0%, var(--accent) 100%);
  border-color: transparent;
  color: #061018;
  font-weight: 600;
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; }

.foot {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.35rem 0 0.55rem;
  font-size: 0.75rem;
  color: var(--muted);
}
.foot a { color: var(--accent); text-decoration: none; }
.foot a:hover { text-decoration: underline; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}
