@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap');

:root {
  --bg0: #071018;
  --bg1: #0c1824;
  --panel: rgba(14, 28, 42, 0.94);
  --panel-solid: #102030;
  --panel2: #152838;
  --text: #eef4f8;
  --muted: #8aa0b4;
  --accent: #2eb8a6;
  --accent-dim: rgba(46, 184, 166, 0.16);
  --accent2: #5ec8ff;
  --warn: #e8a645;
  --danger: #e86a78;
  --ok: #3ecf8e;
  --border: rgba(180, 210, 230, 0.1);
  --border-strong: rgba(180, 210, 230, 0.18);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.4);
  --radius: 14px;
  --radius-sm: 10px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --topbar-h: 52px;
  --font: 'IBM Plex Sans', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  --composer-h: auto;
  --vvh: 100dvh;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg0);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
body {
  background:
    radial-gradient(900px 480px at 8% -8%, rgba(46, 184, 166, 0.12), transparent 55%),
    radial-gradient(700px 420px at 100% 0%, rgba(94, 200, 255, 0.08), transparent 50%),
    linear-gradient(165deg, var(--bg0), var(--bg1) 45%, #08141c);
}
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent2); }
.hidden { display: none !important; }

.app {
  display: flex;
  flex-direction: column;
  height: var(--vvh);
  max-height: var(--vvh);
  width: 100%;
  overflow: hidden;
  position: relative;
}

/* ── Topbar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
  min-height: var(--topbar-h);
  padding: calc(6px + var(--safe-t)) 12px 6px;
  border-bottom: 1px solid var(--border);
  background: rgba(7, 16, 24, 0.92);
  backdrop-filter: blur(14px);
  z-index: 30;
}
.brand {
  font-weight: 700;
  letter-spacing: 0.02em;
  font-size: 1.02rem;
  white-space: nowrap;
}
.brand span { color: var(--accent); }
.topbar .actions { display: flex; gap: 6px; align-items: center; }
.btn {
  border: 1px solid var(--border-strong);
  background: var(--panel2);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  white-space: nowrap;
  line-height: 1.2;
}
.btn:hover { border-color: var(--accent); }
.btn.primary {
  background: linear-gradient(135deg, #1fa896, #2eb8a6 50%, #3ac4b0);
  border: none;
  color: #041210;
  font-weight: 600;
}
.btn.danger {
  background: rgba(232, 106, 120, 0.12);
  border-color: rgba(232, 106, 120, 0.4);
  color: #ffc4cb;
}
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-icon {
  padding: 8px;
  min-width: 40px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ── Auth ── */
.center {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  min-height: 0;
  padding: 20px 16px calc(20px + var(--safe-b));
  overflow: auto;
}
.card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 20px;
  padding: 24px 22px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 8px; font-size: 1.45rem; }
.muted { color: var(--muted); line-height: 1.45; }
.code {
  font-family: var(--mono);
  font-size: clamp(1.35rem, 5vw, 1.7rem);
  letter-spacing: 0.2em;
  background: var(--panel2);
  border: 1px solid var(--border);
  padding: 14px 18px;
  border-radius: 12px;
  display: inline-block;
  margin: 14px 0;
}
.error { color: var(--danger); margin-top: 10px; font-size: 0.9rem; }
.ok { color: var(--ok); }

/* ── Layout ── */
.layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  flex: 1;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  width: 100%;
}
.sidebar {
  border-right: 1px solid var(--border);
  background: rgba(8, 18, 28, 0.96);
  overflow-x: hidden;
  overflow-y: auto;
  padding: 12px 10px calc(16px + var(--safe-b));
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}
.sidebar h3 {
  margin: 4px 8px 8px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 600;
}
.sidebar-section { margin-top: 16px; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  margin-bottom: 4px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 8px 10px;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.nav-item:hover {
  background: rgba(255, 255, 255, 0.04);
  border-color: var(--border);
}
.nav-item.active {
  background: var(--accent-dim);
  border-color: rgba(46, 184, 166, 0.35);
}
.nav-icon {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1.05rem;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border);
}
.nav-meta { min-width: 0; flex: 1; }
.nav-title {
  font-size: 0.9rem;
  font-weight: 560;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-sub {
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 35;
  opacity: 0;
  pointer-events: none;
}
.sidebar-backdrop.open {
  display: block;
  pointer-events: auto;
}

.main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  width: 100%;
  overflow: hidden;
  background: rgba(7, 14, 22, 0.4);
}

/* ── Room header ── */
.room-header {
  border-bottom: 1px solid var(--border);
  background: rgba(10, 20, 30, 0.88);
  padding: 8px 12px 10px;
  flex-shrink: 0;
}
.status-bar {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  align-items: center;
}
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.68rem;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--muted);
}
.status-pill.on { border-color: rgba(46, 184, 166, 0.4); color: var(--accent); }
.status-pill.warn { border-color: rgba(232, 166, 69, 0.45); color: var(--warn); }

.agent-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
  min-height: 48px;
}
.agent-banner.working {
  border-color: rgba(46, 184, 166, 0.35);
  background: linear-gradient(120deg, rgba(46, 184, 166, 0.12), rgba(94, 200, 255, 0.05));
}
.agent-banner.done {
  border-color: rgba(62, 207, 142, 0.3);
  background: rgba(62, 207, 142, 0.07);
}
.agent-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.agent-banner.working .agent-dot { background: var(--accent); }
.agent-banner-text { min-width: 0; flex: 1; }
.agent-banner-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.agent-banner-name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.agent-banner-name .handle {
  color: var(--accent);
  font-family: var(--mono);
  font-size: 0.9rem;
}

/* ── Feed ── */
.feed {
  flex: 1 1 auto;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 10px 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
  width: 100%;
}

.day-sep {
  align-self: center;
  margin: 6px 0;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  color: #c5d2e0;
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  user-select: none;
}

.msg-row {
  display: flex;
  gap: 8px;
  max-width: min(720px, 100%);
  align-items: flex-end;
}
.msg-row.user { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.member { align-self: flex-start; }

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1rem;
  border: 1px solid var(--border-strong);
  background: var(--panel2);
}

.bubble {
  position: relative;
  max-width: 100%;
  min-width: 72px;
  padding: 6px 10px 4px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel-solid);
  word-break: break-word;
  line-height: 1.35;
  font-size: 0.95rem;
}
.msg-row.user .bubble {
  background: linear-gradient(145deg, rgba(46, 184, 166, 0.22), rgba(30, 90, 110, 0.28));
  border-color: rgba(46, 184, 166, 0.3);
  border-bottom-right-radius: 4px;
}
.msg-row.member .bubble {
  border-bottom-left-radius: 4px;
}
.bubble-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 2px;
}
.bubble-author {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent2);
}
.bubble-body {
  white-space: pre-wrap;
  display: inline;
  padding-right: 0;
}
/* Telegram-like: time sits on the last line */
.bubble-foot {
  display: inline-flex;
  float: right;
  margin: 4px 0 0 10px;
  align-items: center;
  height: 16px;
}
.bubble-time {
  font-size: 0.68rem;
  color: rgba(180, 200, 220, 0.78);
  font-family: var(--mono);
  line-height: 1;
  user-select: none;
  white-space: nowrap;
}
.msg-row.user .bubble-time { color: rgba(200, 230, 220, 0.85); }
.bubble::after {
  content: '';
  display: table;
  clear: both;
}

.agent-card {
  align-self: stretch;
  max-width: min(520px, 100%);
  border-radius: 16px;
  border: 1px solid var(--border-strong);
  background: linear-gradient(145deg, rgba(20, 36, 52, 0.95), rgba(12, 24, 36, 0.98));
  padding: 14px;
  overflow: hidden;
  position: relative;
}
.agent-card-glow {
  position: absolute;
  inset: -40% auto auto -20%;
  width: 160px;
  height: 160px;
  border-radius: 50%;
  opacity: 0.35;
  filter: blur(30px);
  pointer-events: none;
}
.agent-card-top {
  display: flex;
  gap: 12px;
  align-items: center;
  position: relative;
  z-index: 1;
}
.agent-card-icon {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.agent-card-name {
  font-weight: 700;
  font-size: 1.05rem;
}
.agent-card-handle {
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--muted);
}
.agent-card-help {
  margin-top: 10px;
  font-size: 0.88rem;
  color: #c8d6e4;
  line-height: 1.45;
  position: relative;
  z-index: 1;
}
.agent-card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  position: relative;
  z-index: 1;
}
.agent-tag {
  font-size: 0.72rem;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
}

.bubble.sys {
  align-self: stretch;
  max-width: 100%;
  background: transparent;
  border: none;
  padding: 0;
  min-width: 0;
}

.spoiler {
  border: 1px dashed var(--border-strong);
  border-radius: 12px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.02);
}
.spoiler-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 11px;
  background: transparent;
  border: none;
  color: #b7c6d6;
  cursor: pointer;
  font-size: 0.78rem;
  text-align: left;
  font-family: var(--mono);
}
.spoiler-toggle:hover { color: var(--text); background: rgba(255,255,255,0.03); }
.spoiler-chevron { font-size: 0.65rem; opacity: 0.7; transition: transform 0.2s; }
.spoiler.open .spoiler-chevron { transform: rotate(90deg); }
.spoiler-body { overflow: hidden; height: 0; }
.spoiler-body-inner {
  padding: 0 12px 10px;
  font-size: 0.78rem;
  color: var(--muted);
  font-family: var(--mono);
  white-space: pre-wrap;
  word-break: break-word;
}

.pending {
  background: rgba(232, 166, 69, 0.1);
  border: 1px solid rgba(232, 166, 69, 0.35);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin: 0 10px;
  flex-shrink: 0;
}
.pending .btn { margin: 6px 6px 0 0; }

/* ── Composer (Telegram-like) ── */
.composer {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(8px + var(--safe-b));
  background: rgba(7, 16, 24, 0.96);
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}
.chips {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding: 0 2px;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--accent2);
  border-radius: 999px;
  padding: 4px 10px 4px 6px;
  cursor: pointer;
  font-size: 0.78rem;
  font-family: var(--mono);
}
.chip-ico {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  background: rgba(255,255,255,0.06);
}

.composer-main {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
}
.composer-tools {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  padding-bottom: 2px;
}
.composer-input-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: flex-end;
  background: var(--panel2);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 6px 8px 6px 14px;
  gap: 6px;
}
.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 24px;
  max-height: 120px;
  height: 24px;
  resize: none;
  background: transparent;
  border: none;
  padding: 6px 0;
  line-height: 1.35;
  outline: none;
  overflow-y: auto;
  field-sizing: content;
}
.composer-input-wrap:focus-within {
  border-color: rgba(46, 184, 166, 0.5);
}
.btn-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, #1fa896, #2eb8a6);
  color: #041210;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
}
.btn-send:disabled { opacity: 0.45; }

/* ── Mobile ── */
@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
    width: 100%;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(88vw, 320px);
    max-width: 320px;
    z-index: 40;
    transform: translateX(-105%);
    visibility: hidden;
    box-shadow: var(--shadow);
    padding-top: calc(14px + var(--safe-t));
  }
  .sidebar.open { visibility: visible; }
  .sidebar-backdrop.open { display: block; }
  .main { width: 100%; max-width: 100vw; }
  .feed { padding: 8px 8px 12px; }
  .msg-row { max-width: 92%; }
  .msg-row .avatar { width: 32px; height: 32px; font-size: 0.9rem; }
  .bubble { font-size: 0.92rem; padding: 7px 10px 5px; }
  .room-header { padding: 8px 10px; }
  .agent-banner { padding: 8px 10px; min-height: 44px; }
  .topbar .actions .btn:not(.btn-icon) {
    padding: 7px 9px;
    font-size: 0.82rem;
  }
  .composer { padding: 6px 8px calc(6px + var(--safe-b)); }
  .composer textarea { max-height: 100px; }
}

@media (min-width: 861px) {
  .btnMenuMobile { display: none !important; }
  .sidebar-backdrop { display: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  .spoiler-chevron { transition: none; }
}

/* ── Control strip / runtime / tools ── */
.control-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  align-items: stretch;
}
.runtime-bar, .tools-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.03);
}
.runtime-bar { flex: 1 1 280px; }
.tools-bar { flex: 0 1 auto; }
.runtime-field {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1 1 120px;
}
.runtime-field span {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.runtime-field select {
  appearance: none;
  background: var(--panel2);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 6px 28px 6px 8px;
  color: var(--text);
  font-size: 0.82rem;
  max-width: 100%;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 14px) 50%, calc(100% - 9px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.runtime-field select:focus {
  outline: none;
  border-color: rgba(46, 184, 166, 0.5);
}
.runtime-badge {
  font-size: 0.68rem;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid rgba(46, 184, 166, 0.35);
  color: var(--accent);
  background: var(--accent-dim);
  white-space: nowrap;
}
.runtime-badge.default {
  border-color: var(--border);
  color: var(--muted);
  background: transparent;
}
.btn-sm {
  padding: 6px 10px;
  font-size: 0.78rem;
  min-height: 32px;
}

/* Banner spoiler / roster */
.agent-banner-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: transparent;
  border: none;
  padding: 0;
  color: inherit;
  cursor: pointer;
  text-align: left;
}
.agent-banner-toggle:hover .agent-banner-name { color: var(--accent2); }
.agent-banner-chevron {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--muted);
  transition: transform 0.2s;
  flex-shrink: 0;
}
.agent-banner.open .agent-banner-chevron { transform: rotate(180deg); }
.agent-roster {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.agent-roster.hidden { display: none !important; }
.roster-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 10px;
  background: rgba(0,0,0,0.18);
  border: 1px solid transparent;
}
.roster-row.active {
  border-color: rgba(46, 184, 166, 0.35);
  background: rgba(46, 184, 166, 0.1);
}
.roster-row.done {
  border-color: rgba(62, 207, 142, 0.25);
}
.roster-ico {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  flex-shrink: 0;
}
.roster-meta { min-width: 0; flex: 1; }
.roster-name {
  font-size: 0.85rem;
  font-weight: 600;
  font-family: var(--mono);
}
.roster-help {
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.roster-status {
  font-size: 0.68rem;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  flex-shrink: 0;
}
.roster-status.working {
  color: var(--accent);
  border-color: rgba(46,184,166,0.4);
}
.roster-status.done {
  color: var(--ok);
  border-color: rgba(62,207,142,0.4);
}

@media (max-width: 860px) {
  .control-strip { flex-direction: column; }
  .runtime-bar, .tools-bar { width: 100%; }
  .tools-bar { justify-content: flex-start; }
  .runtime-field { flex: 1 1 45%; }
}
