/*
 * Threads: conversation list, the open thread with Jethro, and the rights/tools rail.
 * Builds on application.css tokens; Jethro's bubbles are glass panels, the person's are the
 * royal-to-violet ribbon.
 */

/* The screen fills the viewport under the topbar; only the conversation scrolls. */
.page--threads { display: flex; flex-direction: column; height: calc(100vh - 57px); min-height: 620px; padding-bottom: 18px; }
.page--threads .page-header { margin-bottom: 16px; }
.page--threads .flash-stack { margin-bottom: 12px; }

.threads {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 270px minmax(0, 1fr) 290px;
  gap: 18px;
}

/* ---------- Conversation list ---------- */

.threads-list { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.threads-list-items { overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 4px; }

.thread-item {
  display: grid; grid-template-columns: 34px minmax(0, 1fr) auto; gap: 10px; align-items: center;
  padding: 9px 10px; border-radius: var(--radius-sm); border: 1px solid transparent; color: var(--text);
  transition: background 0.15s, border-color 0.15s;
}
.thread-item:hover { background: var(--surface-hover); color: var(--white); }
.thread-item--active { background: var(--ribbon-soft); border-color: var(--border-strong); box-shadow: inset 3px 0 0 var(--cyan); }
.thread-item-icon { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: rgba(1, 9, 45, 0.6); border: 1px solid var(--border); color: var(--cyan); overflow: hidden; }
.thread-item-icon img { width: 100%; height: 100%; object-fit: cover; }
.thread-item-icon svg { width: 17px; height: 17px; }
.thread-item-text { display: flex; flex-direction: column; min-width: 0; }
.thread-item-title { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item-time { font-size: 11px; font-family: var(--font-mono); align-self: start; padding-top: 2px; }

/* ---------- The open thread ---------- */

.thread { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }

.thread-head {
  display: flex; align-items: center; gap: 12px; padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(23, 66, 149, 0.28), transparent);
}
.thread-head-icon { position: relative; width: 40px; height: 40px; flex: none; }
.thread-head-icon img { width: 40px; height: 40px; border-radius: 11px; box-shadow: 0 0 22px rgba(67, 183, 253, 0.45); }
.thread-head-icon .dot { position: absolute; right: -2px; bottom: -2px; box-shadow: 0 0 0 2px var(--navy-900), 0 0 8px var(--ok); }
.thread-head-title h2 { font-size: 17px; }
.thread-head-title .faint { font-size: 12.5px; }

.thread-messages { flex: 1; min-height: 0; overflow-y: auto; padding: 22px 22px 10px; scroll-behavior: smooth; }
.thread-stream { display: flex; flex-direction: column; gap: 18px; max-width: 880px; margin: 0 auto; }

/* Messages */

.msg { display: flex; gap: 12px; align-items: flex-start; max-width: 88%; animation: msg-in 0.28s ease-out; }
@keyframes msg-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg-avatar { flex: none; padding-top: 18px; }
.msg-avatar img { width: 34px; height: 34px; border-radius: 10px; box-shadow: 0 0 18px rgba(67, 183, 253, 0.35); display: block; }
.msg-body { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.msg-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; }
.msg-author { font-family: var(--font-display); font-weight: 600; font-size: 13.5px; color: var(--white); }
.msg-meta time { font-family: var(--font-mono); font-size: 11px; }

.bubble { padding: 11px 15px; border-radius: 16px; line-height: 1.55; overflow-wrap: anywhere; }
.bubble p { margin: 0; }
.bubble p + p, .bubble p + ul, .bubble p + ol, .bubble ul + p, .bubble ol + p { margin-top: 8px; }
.bubble ul, .bubble ol { margin: 0; padding-left: 20px; }
.bubble li + li { margin-top: 3px; }
.bubble code { background: rgba(1, 9, 45, 0.55); padding: 1px 5px; border-radius: 5px; }

.bubble--jethro {
  background: linear-gradient(180deg, rgba(14, 32, 106, 0.72), rgba(6, 26, 94, 0.6));
  border: 1px solid var(--border-strong); border-top-left-radius: 5px;
  box-shadow: 0 0 0 1px rgba(67, 183, 253, 0.06), 0 8px 28px rgba(1, 9, 45, 0.45), 0 0 24px rgba(68, 73, 253, 0.1);
}

.msg--person { margin-left: auto; flex-direction: row-reverse; }
.msg--person .msg-body { align-items: flex-end; }
.msg--person .msg-avatar { padding-top: 18px; }
.bubble--person {
  background: linear-gradient(135deg, var(--royal) 0%, #2c4fd6 55%, var(--violet) 100%);
  color: var(--white); border-top-right-radius: 5px;
  box-shadow: 0 8px 26px rgba(68, 73, 253, 0.28);
}

.msg--failed .bubble--jethro, .msg--refused .bubble--jethro { border-color: rgba(255, 93, 122, 0.45); background: rgba(255, 93, 122, 0.07); }
.msg--refused .bubble--jethro { border-color: rgba(255, 200, 87, 0.45); background: rgba(255, 200, 87, 0.07); }

.msg-foot { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 12px; }
.msg-foot--hint { font-size: 12.5px; }
.msg-foot[hidden] { display: none; }
.msg-cost { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; color: var(--text-faint); font-size: 11.5px; }

.chip svg { width: 13px; height: 13px; flex: none; }
.chip--cite { color: var(--sky); border-color: rgba(67, 183, 253, 0.4); background: rgba(67, 183, 253, 0.1); }
a.chip--cite:hover { color: var(--white); border-color: var(--cyan); box-shadow: 0 0 14px rgba(67, 183, 253, 0.35); }
.chip--plain { color: var(--text-faint); background: transparent; border-style: dashed; }
.chip--tool { color: var(--text-muted); background: rgba(1, 9, 45, 0.45); font-size: 11.5px; padding: 3px 9px; }
.chip--tool .num { color: var(--cyan); }
.chip--tool-error { color: var(--warn); border-color: rgba(255, 200, 87, 0.35); }
.chip--model { font-family: var(--font-mono); font-size: 11px; padding: 2px 8px; }
.chip--haiku { color: var(--cyan); border-color: rgba(67, 183, 253, 0.4); }
.chip--opus { color: var(--violet-soft); border-color: rgba(68, 73, 253, 0.5); background: rgba(68, 73, 253, 0.16); }
.chip--sonnet { color: var(--ok); border-color: rgba(62, 230, 168, 0.35); }

/* Thinking placeholder */

.bubble--pending { display: inline-flex; align-items: center; gap: 10px; color: var(--text-muted); position: relative; overflow: hidden; }
.bubble--pending::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(100deg, transparent 20%, rgba(67, 183, 253, 0.12) 50%, transparent 80%);
  transform: translateX(-100%); animation: sweep 1.8s ease-in-out infinite;
}
@keyframes sweep { to { transform: translateX(100%); } }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--cyan); box-shadow: 0 0 8px var(--cyan); animation: typing 1.2s ease-in-out infinite; }
.typing i:nth-child(2) { animation-delay: 0.15s; background: var(--blue); }
.typing i:nth-child(3) { animation-delay: 0.3s; background: var(--violet); }
@keyframes typing { 0%, 80%, 100% { opacity: 0.25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }
.typing-label { font-size: 13px; font-family: var(--font-mono); }
.msg--stalled .typing, .msg--stalled .bubble--pending::after { display: none; }

/* Empty thread */

.thread-empty { text-align: center; padding: 34px 10px 10px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.thread-empty-mark img { width: 72px; height: 72px; border-radius: 20px; box-shadow: 0 0 46px rgba(67, 183, 253, 0.45); }
.thread-empty h3 { font-size: 22px; }
.thread-empty p { max-width: 520px; }
.starters { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: min(640px, 100%); margin-top: 8px; }
.starter-form { display: block; }
.starter {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left; cursor: pointer;
  padding: 12px 14px; border-radius: var(--radius-sm); font: 500 13.5px/1.35 var(--font-body); color: var(--text);
  background: rgba(14, 32, 106, 0.45); border: 1px solid var(--border);
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s, transform 0.1s;
}
.starter svg { width: 16px; height: 16px; color: var(--cyan); flex: none; }
.starter:hover { border-color: var(--cyan); background: var(--surface-hover); box-shadow: 0 0 20px rgba(67, 183, 253, 0.2); color: var(--white); }
.starter:active { transform: translateY(1px); }

/* Composer */

.composer { padding: 12px 18px 14px; border-top: 1px solid var(--border); background: rgba(1, 9, 45, 0.45); }
.composer-errors { margin-bottom: 8px; }
.composer-box {
  display: flex; align-items: flex-end; gap: 10px; max-width: 880px; margin: 0 auto;
  padding: 8px 8px 8px 14px; border-radius: 14px; border: 1px solid var(--border-strong); background: rgba(1, 9, 45, 0.7);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer-box:focus-within { border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(67, 183, 253, 0.16), 0 0 30px rgba(68, 73, 253, 0.18); }
.composer-box textarea {
  flex: 1; resize: none; border: 0; outline: none; background: transparent; color: var(--text);
  font: 14.5px/1.5 var(--font-body); padding: 6px 0; max-height: 200px; min-height: 24px;
}
.composer-box textarea::placeholder { color: var(--text-faint); }
.composer-send { width: 38px; height: 38px; padding: 0; border-radius: 11px; flex: none; }
.composer-send svg { width: 17px; height: 17px; }
.composer-hint { max-width: 880px; margin: 7px auto 0; font-size: 11.5px; }

/* ---------- Context rail ---------- */

.thread-context { display: flex; flex-direction: column; gap: 14px; overflow-y: auto; min-height: 0; }
.thread-context .panel-header { padding: 11px 16px; }
.thread-context .panel-header svg { width: 16px; height: 16px; color: var(--cyan); }
.thread-context .panel-body { padding: 12px 16px; }
.thread-context .hint { margin: 0; }
.thread-context .panel-footer { padding: 10px 16px; line-height: 1.45; }
.tool-list { list-style: none; margin: 0; padding: 6px 8px 8px; }
.tool-row { display: flex; align-items: center; gap: 9px; padding: 6px 8px; border-radius: 8px; font-size: 12.5px; }
.tool-row .dot { width: 7px; height: 7px; background: var(--text-faint); box-shadow: none; }
.tool-row .dot--ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.tool-row--off { color: var(--text-faint); }
.tool-row--off .mono { text-decoration: line-through; text-decoration-color: rgba(96, 119, 171, 0.6); }
.tool-row svg { width: 13px; height: 13px; }
.routing .row { flex-wrap: nowrap; align-items: flex-start; font-size: 12.5px; }
.routing .chip { flex: none; }
.thread-spend { font-size: 24px; }

/* ---------- Group threads and conversations ---------- */

.thread-context-rail { display: flex; flex-direction: column; gap: 14px; }
.thread-head-icon--group { display: grid; place-items: center; border-radius: 11px; background: var(--ribbon-soft); border: 1px solid var(--border-strong); color: var(--cyan); }
.thread-head-icon--group svg { width: 20px; height: 20px; }

/* Other people's messages sit on the left in a glass bubble; your own stay on the right. */
.msg--person:not(.msg--mine) { margin-left: 0; flex-direction: row; }
.msg--person:not(.msg--mine) .msg-body { align-items: flex-start; }
.msg--person:not(.msg--mine) .bubble--person {
  background: var(--surface-strong); border: 1px solid var(--border); color: var(--text);
  border-top-right-radius: 16px; border-top-left-radius: 5px; box-shadow: none;
}
.bubble--hidden { display: inline-flex; align-items: center; gap: 8px; color: var(--text-faint); font-style: italic; border: 1px dashed var(--border); background: transparent; }
.bubble--hidden svg { width: 13px; height: 13px; }

.thread-banner { display: flex; gap: 10px; align-items: flex-start; margin: 12px 18px 0; font-size: 13px; line-height: 1.5; }
.thread-banner svg { width: 16px; height: 16px; flex: none; margin-top: 2px; color: var(--warn); }

.msg-actions { position: relative; font-size: 12px; }
.msg-actions summary { list-style: none; cursor: pointer; padding: 0 6px; border-radius: 6px; letter-spacing: 1px; }
.msg-actions summary::-webkit-details-marker { display: none; }
.msg-actions summary:hover { color: var(--white); background: var(--surface-hover); }
.msg-actions-menu { position: absolute; z-index: 5; top: 20px; left: 0; width: 250px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.msg--mine .msg-actions-menu { left: auto; right: 0; }

.participant-list { list-style: none; margin: 0; padding: 6px 8px 8px; }
.participant-row { display: flex; align-items: center; gap: 9px; padding: 6px 8px; border-radius: 8px; font-size: 13px; }
.participant-row .avatar--jethro { overflow: hidden; padding: 0; }
.participant-row .avatar--jethro img { width: 100%; height: 100%; object-fit: cover; }
.participant-note { font-size: 11.5px; }
.participant-form .row { flex-wrap: nowrap; }
.participant-form input[type="text"] { min-width: 0; flex: 1; }
.participant-add summary, .new-conversation summary { list-style: none; cursor: pointer; }
.participant-add summary::-webkit-details-marker, .new-conversation summary::-webkit-details-marker { display: none; }
.participant-add[open] summary { margin-bottom: 10px; }

.pick-list { max-height: 220px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 6px; display: flex; flex-direction: column; gap: 2px; background: rgba(1, 9, 45, 0.35); }
.pick-row { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 6px; font-size: 13px; cursor: pointer; }
.pick-row:hover { background: var(--surface-hover); }

.new-conversation { position: relative; }
.new-conversation-menu { position: absolute; z-index: 20; right: 0; top: calc(100% + 8px); width: min(340px, calc(100vw - 32px)); padding: 14px; }

.thread-item--unread .thread-item-title { color: var(--white); }
.thread-item-time { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.thread-item-unread { min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--cyan); color: var(--navy-900); font-size: 10.5px; font-weight: 700; display: grid; place-items: center; }

@media (max-width: 1280px) {
  .threads { grid-template-columns: 250px minmax(0, 1fr); }
  .thread-context { display: none; }
}

@media (max-width: 860px) {
  .page--threads { height: auto; }
  .threads { grid-template-columns: 1fr; }
  .threads-list { max-height: 220px; }
  .thread { height: calc(100vh - 120px); min-height: 480px; }
  .msg { max-width: 100%; }
  .starters { grid-template-columns: 1fr; }
}

.thread-empty-mark--group { width: 64px; height: 64px; border-radius: 18px; display: grid; place-items: center; background: var(--ribbon-soft); border: 1px solid var(--border-strong); color: var(--cyan); }
.thread-empty-mark--group svg { width: 28px; height: 28px; }

/* Narrower screens drop the rights rail; the people in a group thread or conversation stay,
   as a row under the thread. */
@media (max-width: 1280px) {
  .threads { grid-template-rows: minmax(0, 1fr) auto; }
  .threads > .threads-list { grid-column: 1; grid-row: 1; }
  .threads > .thread { grid-column: 2; grid-row: 1 / span 2; }
  .thread-context:has(#participants) { display: flex; grid-column: 1; grid-row: 2; max-height: 48vh; }
  .thread-context .thread-context-rail { display: none; }
}

@media (max-width: 860px) {
  .threads { grid-template-rows: none; }
  .threads > .threads-list, .threads > .thread, .thread-context:has(#participants) { grid-column: auto; grid-row: auto; max-height: none; }
  .thread-head { flex-wrap: wrap; row-gap: 8px; }
  .thread-head-title { min-width: 0; flex: 1 1 60%; }
  .new-conversation-menu { right: auto; left: 0; }
}
