/* ============================================================
   Relational norms (beta) — front of house (chat, light) / back of house
   (instrument panels, dark). Function colors follow the paper:
   care=rose, hierarchy=charcoal, mating=green, transaction=blue.
   ============================================================ */

:root {
  --paper: #f2f3f5;        /* chat side ground */
  --card: #ffffff;
  --ink: #20222b;
  --ink-soft: #6a6e7e;
  --line: #dfe1e7;

  --panel: #131419;        /* instrument side ground */
  --panel-card: #1b1d25;
  --trace: #b9bdcc;
  --trace-dim: #6c7080;
  --panel-line: #2a2d38;

  --care: #e8798f;
  --hierarchy: #8b8e9c;
  --mating: #5cae6e;
  --transaction: #6f9fd8;

  --display: "Fraunces", Georgia, serif;
  --body: "Public Sans", -apple-system, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: var(--body);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
}

.screen { display: none; }
.screen.active { display: block; }

button { font-family: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible, .role-chip:focus-visible, .role-mini:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* ============ SETUP ============ */

#screenSetup.active {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: clamp(24px, 5vh, 64px) 24px 48px;
}

.masthead { text-align: center; max-width: 620px; }
.wordmark {
  font-family: var(--display);
  font-weight: 640;
  font-size: clamp(44px, 7vw, 72px);
  letter-spacing: -0.02em;
  line-height: 1;
}
.wordmark em { font-style: italic; font-weight: 400; }
.beta-tag {
  display: inline-block;
  vertical-align: super;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  background: var(--card);
}
.masthead-sub {
  margin-top: 14px;
  color: var(--ink-soft);
  font-size: 14.5px;
  line-height: 1.55;
}
.masthead-sub .cite { font-style: italic; }
.byline {
  margin-top: 8px;
  font-size: 12.5px;
  color: var(--ink-soft);
  opacity: .8;
  letter-spacing: .015em;
}

.fn-legend {
  margin-top: 18px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px 16px;
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-soft);
}
.fn-legend .fn { display: inline-flex; align-items: center; gap: 6px; }
.fn-legend .dot { width: 9px; height: 9px; border-radius: 50%; }

.mode-grid {
  margin-top: clamp(28px, 5vh, 52px);
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 460px));
  justify-content: center;
  gap: 24px;
  width: 100%;
  max-width: 1000px;
}

.mode-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 26px 26px 24px;
  display: flex;
  flex-direction: column;
}
.mode-eyebrow {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--ink-soft);
}
.mode-card h2 {
  font-family: var(--display);
  font-weight: 560;
  font-size: 30px;
  margin-top: 8px;
}
.mode-card > p {
  margin-top: 8px;
  color: var(--ink-soft);
  font-size: 14px;
  line-height: 1.55;
}
.picker-label {
  margin-top: 20px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-soft);
}

.role-chips { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 8px; }
.role-chip {
  border: 1.5px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--ink);
  transition: border-color .15s, background .15s;
}
.role-chip[aria-pressed="true"] {
  border-color: var(--chip-accent, var(--ink));
  background: color-mix(in srgb, var(--chip-accent, var(--ink)) 10%, white);
}
.role-chip .chip-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--chip-accent); margin-right: 7px;
}

.role-cards { margin-top: 10px; display: grid; gap: 8px; }
.role-mini {
  display: grid;
  grid-template-columns: 1fr 118px;
  gap: 4px 14px;
  align-items: center;
  text-align: left;
  border: 1.5px solid var(--line);
  background: var(--card);
  border-radius: 10px;
  padding: 10px 14px;
  transition: border-color .15s, background .15s;
}
.role-mini[aria-pressed="true"] {
  border-color: var(--chip-accent, var(--ink));
  background: color-mix(in srgb, var(--chip-accent, var(--ink)) 8%, white);
}
.role-mini .rm-name { font-weight: 600; font-size: 14.5px; }
.role-mini .rm-tag { grid-column: 1; color: var(--ink-soft); font-size: 12px; line-height: 1.4; }
.profile-line {
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--ink-soft);
  line-height: 1.7;
}
.profile-line .pl-fn { white-space: nowrap; }
.profile-line .pl-fn b { font-weight: 600; }
.profile-line .pl-sep { opacity: .5; }
.role-mini .rm-bars { grid-column: 2; grid-row: 1 / span 2; }

/* norm profile bars: center axis, left = proscribed, right = prescribed */
.norm-bars { display: grid; gap: 3px; }
.norm-row { display: grid; grid-template-columns: 10px 1fr; align-items: center; gap: 6px; }
.norm-row .nb-label {
  font-family: var(--mono); font-size: 9px; color: var(--ink-soft);
  text-transform: uppercase;
}
.nb-track {
  position: relative; height: 6px; border-radius: 3px;
  background: color-mix(in srgb, var(--line) 60%, transparent);
}
.nb-track::after {
  content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px;
  width: 1px; background: var(--line);
}
.nb-fill { position: absolute; top: 0; bottom: 0; border-radius: 3px; }

.go {
  margin-top: 22px;
  border: none;
  border-radius: 10px;
  background: var(--ink);
  color: #fff;
  font-size: 14.5px;
  font-weight: 600;
  padding: 12px;
  transition: opacity .15s, transform .1s;
}
.go:disabled { opacity: 0.35; cursor: default; }
.go:not(:disabled):hover { transform: translateY(-1px); }

/* ============ ROOM ============ */

#screenRoom.active { height: 100dvh; }
.room-shell {
  height: 100%;
  display: grid;
  grid-template-columns: minmax(420px, 58%) 1fr;
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
}
.room-shell.solo-panel { grid-template-columns: minmax(420px, 62%) 1fr; }

/* ---- chat side (front of house) ---- */
.chat-side {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--paper);
  border-right: 1px solid var(--line);
}
.chat-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.leave {
  border: 1px solid var(--line); background: var(--card);
  border-radius: 8px; width: 32px; height: 32px; font-size: 15px;
  color: var(--ink-soft); flex: none;
}
.leave:hover { color: var(--ink); }
.chat-title { min-width: 0; }
.chat-title-main { font-family: var(--display); font-weight: 560; font-size: 19px; }
.chat-title-sub { font-size: 12px; color: var(--ink-soft); margin-top: 1px; }
.roster { margin-left: auto; display: flex; gap: 6px; }
.roster .badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600;
  border-radius: 999px; padding: 5px 11px;
  background: color-mix(in srgb, var(--b-accent) 12%, white);
  color: color-mix(in srgb, var(--b-accent) 70%, black);
}
.roster .badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--b-accent); }
.roster .badge .role { font-weight: 400; opacity: .75; }

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 20px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.msg { max-width: 76%; animation: rise .22s ease both; }
.msg .msg-meta {
  font-size: 11.5px; font-weight: 600; margin: 0 4px 3px;
  color: var(--m-accent, var(--ink-soft));
}
.msg .msg-meta .role-tag {
  font-family: var(--mono); font-weight: 400; font-size: 10px;
  opacity: .7; margin-left: 5px; text-transform: uppercase; letter-spacing: .06em;
}
.msg .bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14.5px;
  line-height: 1.5;
  background: var(--card);
  border: 1px solid var(--line);
  border-top-left-radius: 5px;
  overflow-wrap: break-word;
}
.msg.bot .bubble { border-left: 3px solid var(--m-accent); }
.msg.me { align-self: flex-end; }
.msg.me .bubble {
  background: var(--ink); color: #fff; border-color: var(--ink);
  border-radius: 14px; border-top-right-radius: 5px;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

.typing-row {
  min-height: 26px;
  padding: 0 22px 4px;
  display: flex;
  gap: 14px;
  align-items: center;
}
.typing-pill {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 12px; color: var(--ink-soft);
}
.typing-pill .tdots { display: inline-flex; gap: 3px; }
.typing-pill .tdots i {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--t-accent, var(--ink-soft));
  animation: blink 1.2s infinite;
}
.typing-pill .tdots i:nth-child(2) { animation-delay: .2s; }
.typing-pill .tdots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 70%, 100% { opacity: .25; } 35% { opacity: 1; } }

.composer {
  display: flex;
  gap: 10px;
  padding: 12px 18px calc(14px + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
}
.composer input {
  flex: 1;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 11px 14px;
  font-size: 14.5px;
  font-family: inherit;
  background: var(--paper);
}
.composer input:focus { outline: none; border-color: var(--ink-soft); }
.composer button {
  border: none; border-radius: 10px; background: var(--ink); color: #fff;
  font-weight: 600; font-size: 14px; padding: 0 20px;
}

/* ---- instrument side (back of house) ---- */
.instrument-side {
  display: grid;
  grid-auto-rows: minmax(0, 1fr);
  background: var(--panel);
  min-width: 0;
  min-height: 0;
}
.instrument {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-bottom: 1px solid var(--panel-line);
}
.instrument:last-child { border-bottom: none; }
.instrument-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 12px 18px 10px;
  border-bottom: 1px solid var(--panel-line);
}
.instrument-head .agent-name {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  color: var(--i-accent);
}
.instrument-head .agent-role {
  font-family: var(--mono); font-size: 11px; color: var(--trace-dim);
  text-transform: uppercase; letter-spacing: .08em;
}
.instrument-head .agent-state {
  margin-left: auto;
  font-family: var(--mono); font-size: 10.5px;
  color: var(--trace-dim);
}
.instrument-head .agent-state.live { color: var(--i-accent); }

.instrument-profile {
  padding: 10px 18px;
  border-bottom: 1px solid var(--panel-line);
}
.instrument-profile .norm-row .nb-label { color: var(--trace-dim); }
.instrument-profile .nb-track { background: #262933; }
.instrument-profile .nb-track::after { background: #3a3e4c; }

.trace-feed {
  flex: 1;
  overflow-y: auto;
  padding: 12px 18px 16px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  color: var(--trace);
}
.trace-entry {
  position: relative;
  padding: 2px 0 10px 14px;
  border-left: 2px solid color-mix(in srgb, var(--i-accent) 35%, transparent);
  animation: rise .2s ease both;
}
.trace-entry .t-kind {
  font-size: 9.5px;
  font-weight: 600;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--trace-dim);
  display: block;
}
.trace-entry.k-thinking { border-left-color: var(--i-accent); }
.trace-entry.k-thinking .t-text { color: #e6e8f0; }
.trace-entry.k-thinking .t-kind { color: var(--i-accent); }
.trace-entry.k-sent .t-text { color: var(--trace-dim); font-style: italic; }
.trace-entry.k-skip .t-text { color: var(--trace-dim); }
.trace-entry.k-memory .t-text { color: var(--trace-dim); }
.trace-entry.k-error .t-text, .trace-entry.k-error .t-kind { color: #e07070; }
.trace-empty {
  color: var(--trace-dim);
  font-size: 11.5px;
  padding-top: 6px;
}
.caret::after { content: "▎"; color: var(--i-accent); animation: blink 1s infinite; }

/* ============ responsive & motion ============ */

@media (max-width: 900px) {
  .room-shell, .room-shell.solo-panel { grid-template-columns: 1fr; grid-template-rows: 58% 42%; }
  .chat-side { border-right: none; border-bottom: 1px solid var(--line); }
  .instrument-side { grid-auto-rows: 1fr; grid-auto-flow: column; grid-auto-columns: 1fr; }
  .instrument { border-bottom: none; border-right: 1px solid var(--panel-line); }
  .instrument-profile { display: none; }
  .msg { max-width: 88%; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}