:root {
  --bg: #0e1014;
  --bg-2: #11141a;
  --surface: #181c24;
  --surface-2: #202633;
  --text: #eef0f5;
  --muted: #98a0ae;
  --line: #262c38;
  --accent: #8b7dff;
  --accent-2: #b39dff;
  --green: #3ecf8e;
  --amber: #f5b942;
  --red: #ff6b6b;
  --radius: 18px;
  --shadow: 0 16px 48px rgba(0, 0, 0, .38);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7fb;
    --bg-2: #eef0f6;
    --surface: #ffffff;
    --surface-2: #f1f3f8;
    --text: #171a21;
    --muted: #6b7280;
    --line: #e3e6ed;
    --shadow: 0 16px 48px rgba(30, 35, 50, .12);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body { overflow: hidden; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--red); font-size: .9rem; margin: 10px 0 0; }

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }

.screen { height: 100dvh; }

/* ---------------------------------------------------------------- auth */
.auth-screen {
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 20% -10%, rgba(139, 125, 255, .28), transparent 60%),
    radial-gradient(700px 500px at 100% 110%, rgba(62, 207, 142, .16), transparent 60%),
    var(--bg);
}

.auth-card {
  width: min(420px, 100%);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: 26px;
  padding: 34px 28px 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.logo, .brand-mark {
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border-radius: 14px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff;
  box-shadow: 0 8px 24px rgba(139, 125, 255, .35);
}
.logo svg, .brand-mark svg { width: 30px; height: 30px; }
.auth-card h1 { font-size: 1.9rem; margin: 20px 0 4px; letter-spacing: -.03em; }
.auth-card form { margin-top: 26px; }
.auth-card label { display: block; font-size: .82rem; color: var(--muted); margin: 14px 0 6px; }

input[type=password], input[type=text], textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 15px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
}

.primary {
  width: 100%;
  margin-top: 22px;
  padding: 14px 18px;
  border-radius: 14px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 650;
  box-shadow: 0 10px 26px rgba(139, 125, 255, .3);
}
.primary:active { transform: translateY(1px); }

/* ---------------------------------------------------------------- shell */
#app {
  display: flex;
  flex-direction: column;
  max-width: 820px;
  margin: 0 auto;
  position: relative;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(var(--safe-t) + 14px) 18px 12px;
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { font-size: 1.35rem; margin: 0; letter-spacing: -.03em; }
.brand p { margin: 2px 0 0; font-size: .78rem; }

.icon-btn {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 13px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  flex: none;
}
.icon-btn:hover { color: var(--text); }
.icon-btn svg { width: 21px; height: 21px; }

/* ---------------------------------------------------------------- agent strip */
.agent-strip {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 6px 18px 14px;
  scrollbar-width: none;
}
.agent-strip::-webkit-scrollbar { display: none; }

.agent-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px 9px 9px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  flex: none;
  transition: border-color .15s, transform .1s;
}
.agent-chip:active { transform: scale(.98); }
.agent-chip.active { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.agent-chip .name { font-size: .86rem; font-weight: 600; white-space: nowrap; }
.agent-chip .kind { font-size: .68rem; color: var(--muted); }

.avatar {
  width: 38px; height: 38px;
  border-radius: 13px;
  display: grid; place-items: center;
  font-weight: 700;
  color: #fff;
  font-size: .9rem;
  flex: none;
  position: relative;
}
.avatar .dot {
  position: absolute; right: -2px; bottom: -2px;
  width: 11px; height: 11px; border-radius: 50%;
  border: 2px solid var(--bg);
  background: var(--muted);
}
.avatar .dot.online { background: var(--green); }
.avatar .dot.running { background: var(--amber); }

/* ---------------------------------------------------------------- list */
.list-view { flex: 1; overflow-y: auto; padding: 0 18px calc(110px + var(--safe-b)); }
.list-head { display: flex; align-items: baseline; justify-content: space-between; margin: 6px 2px 10px; }
.list-head h2 { font-size: .78rem; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); margin: 0; font-weight: 650; }
.text-btn { color: var(--accent); font-size: .8rem; font-weight: 600; padding: 4px; }

.convo-list { display: flex; flex-direction: column; gap: 2px; }

.convo {
  display: flex;
  align-items: center;
  gap: 13px;
  padding: 13px 10px;
  border-radius: 16px;
  text-align: left;
  width: 100%;
  transition: background .12s;
}
.convo:hover, .convo:active { background: var(--surface); }
.convo .body { flex: 1; min-width: 0; }
.convo .row1 { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.convo .title { font-weight: 600; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo .time { font-size: .72rem; color: var(--muted); flex: none; }
.convo .preview { font-size: .82rem; color: var(--muted); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo .badge { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }

.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty-mark { font-size: 2.4rem; color: var(--accent); margin-bottom: 8px; }
.empty p { color: var(--text); font-weight: 600; margin: 0 0 4px; }
.empty span { font-size: .85rem; }

.fab {
  position: fixed;
  right: max(20px, calc((100vw - 820px) / 2 + 20px));
  bottom: calc(22px + var(--safe-b));
  width: 58px; height: 58px;
  border-radius: 20px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff;
  font-size: 1.8rem;
  line-height: 1;
  display: grid; place-items: center;
  box-shadow: 0 12px 30px rgba(139, 125, 255, .4);
  z-index: 6;
}
.fab:active { transform: scale(.96); }

/* ---------------------------------------------------------------- chat */
.chat-view {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: calc(var(--safe-t) + 10px) 14px 10px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  backdrop-filter: blur(18px);
}
.chat-head .avatar { width: 36px; height: 36px; border-radius: 12px; }
.chat-title { min-width: 0; }
.chat-title h2 { font-size: 1rem; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title p { margin: 1px 0 0; font-size: .74rem; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.msg { display: flex; flex-direction: column; max-width: 86%; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.msg.system { align-self: center; max-width: 94%; }

.bubble {
  padding: 11px 14px;
  border-radius: 18px;
  font-size: .93rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .bubble {
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff;
  border-bottom-right-radius: 6px;
}
.msg.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}
.msg.system .bubble {
  background: transparent;
  border: 1px dashed var(--line);
  color: var(--muted);
  font-size: .8rem;
  text-align: center;
}
.msg .meta { font-size: .66rem; color: var(--muted); margin: 4px 6px 0; }

.typing .bubble { display: flex; gap: 4px; align-items: center; min-height: 20px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-2px); } }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 9px;
  padding: 10px 12px calc(10px + var(--safe-b));
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
}
.composer textarea {
  resize: none;
  max-height: 160px;
  border-radius: 18px;
  padding: 12px 15px;
  line-height: 1.35;
}
.send-btn {
  width: 46px; height: 46px;
  border-radius: 16px;
  background: linear-gradient(145deg, var(--accent), var(--accent-2));
  color: #fff;
  display: grid; place-items: center;
  flex: none;
  box-shadow: 0 8px 20px rgba(139, 125, 255, .3);
}
.send-btn svg { width: 22px; height: 22px; }
.send-btn:disabled { opacity: .45; box-shadow: none; }

/* ---------------------------------------------------------------- sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(5, 7, 10, .55);
  backdrop-filter: blur(3px);
}
.sheet {
  position: fixed;
  left: 50%; bottom: 0; z-index: 21;
  width: min(560px, 100%);
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 24px 24px 0 0;
  padding: 8px 20px calc(24px + var(--safe-b));
  box-shadow: var(--shadow);
  animation: sheet-in .22s ease-out;
  max-height: 92dvh;
  overflow-y: auto;
}
@keyframes sheet-in { from { transform: translate(-50%, 30px); opacity: .4; } }
.sheet-grab { width: 42px; height: 4px; border-radius: 99px; background: var(--line); margin: 4px auto 16px; }
.sheet h2 { font-size: 1.12rem; margin: 0 0 4px; letter-spacing: -.02em; }
.sheet .sub { color: var(--muted); font-size: .82rem; margin: 0 0 16px; }
.sheet label { display: block; font-size: .8rem; color: var(--muted); margin: 12px 0 6px; }
.sheet .primary { margin-top: 16px; }
.sheet .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 13px 0; border-bottom: 1px solid var(--line); }
.sheet .row:last-of-type { border-bottom: 0; }
.sheet .row .grow { min-width: 0; }
.sheet .row strong { font-size: .92rem; display: block; }
.sheet .row small { color: var(--muted); font-size: .76rem; }
.sheet .danger { color: var(--red); font-weight: 600; font-size: .85rem; }
.sheet .ok { color: var(--green); font-weight: 600; font-size: .85rem; }
.sheet .agent-pick { display: flex; flex-direction: column; gap: 4px; margin: 8px -8px 0; }
.sheet .agent-pick button { display: flex; align-items: center; gap: 11px; padding: 10px 8px; border-radius: 14px; text-align: left; }
.sheet .agent-pick button:hover { background: var(--surface-2); }
.sheet .agent-pick .name { font-weight: 600; font-size: .9rem; }
.sheet .agent-pick .kind { color: var(--muted); font-size: .72rem; }

/* ---------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%; bottom: calc(90px + var(--safe-b));
  transform: translateX(-50%);
  z-index: 50;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 11px 16px;
  border-radius: 14px;
  font-size: .84rem;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 40px);
  animation: toast-in .18s ease-out;
}
@keyframes toast-in { from { transform: translate(-50%, 8px); opacity: 0; } }

@media (min-width: 820px) {
  #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .chat-view { max-width: 820px; left: 50%; transform: translateX(-50%); border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .fab { right: calc((100vw - 820px) / 2 + 20px); }
}
