:root {
  --bg: #0e0f12;
  --panel: #16181d;
  --panel-2: #1d2027;
  --line: #2a2e37;
  --text: #e8e9ee;
  --muted: #8b90a0;
  --accent: #7c8cff;
  --accent-ink: #0e0f12;
  --ok: #4cc38a;
  --warn: #e5b454;
  --err: #ef6b6b;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

#app { height: 100dvh; display: flex; flex-direction: column; }

button, input, textarea, select { font: inherit; color: inherit; }

button {
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* ---------- top bar ---------- */

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 5;
}

.bar h1 {
  flex: 1;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bar .sub { display: block; font-size: 12px; font-weight: 400; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  flex: none;
  color: var(--text);
}
.icon-btn:active { background: var(--panel-2); }
.icon-btn svg { width: 22px; height: 22px; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; flex: none; }
.dot.on { background: var(--ok); }
.dot.run { background: var(--accent); animation: pulse 1.2s infinite; }
.dot.err { background: var(--err); }

@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- tabs ---------- */

.tabs { display: flex; gap: 6px; padding: 10px 12px 4px; }
.tab {
  flex: 1;
  padding: 8px 0;
  border-radius: 10px;
  background: var(--panel);
  color: var(--muted);
  font-weight: 500;
}
.tab.active { background: var(--panel-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }

/* ---------- lists ---------- */

.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; min-height: 0; }

.list { padding: 6px 12px calc(96px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 8px; }

.item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  text-align: left;
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--panel);
}
.item:active { background: var(--panel-2); }
.item .dot { margin-top: 8px; }
.item .body { flex: 1; min-width: 0; }
.item .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .meta { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .preview { font-size: 13.5px; color: #b7bbc8; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.group-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 10px 4px 2px; }

.empty { color: var(--muted); text-align: center; padding: 48px 24px; }

.fab {
  position: fixed;
  right: 16px;
  bottom: calc(18px + var(--safe-bottom));
  height: 54px;
  padding: 0 20px;
  border-radius: 27px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}
.fab svg { width: 20px; height: 20px; }

/* ---------- messages ---------- */

.messages { padding: 12px 12px 20px; display: flex; flex-direction: column; gap: 10px; }

.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end;
  max-width: 88%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 9px 13px;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
}
.msg.user .att { font-size: 12px; color: var(--muted); margin-top: 4px; }
.msg.assistant { padding: 0 2px; }
.msg.assistant p { margin: 0 0 8px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { font-size: 16px; margin: 12px 0 6px; }
.msg.assistant ul, .msg.assistant ol { margin: 4px 0 8px; padding-left: 22px; }
.msg.assistant a { color: var(--accent); }
.msg.assistant code {
  font: 13px/1.45 ui-monospace, "Cascadia Code", Consolas, monospace;
  background: var(--panel-2);
  padding: 1px 5px;
  border-radius: 5px;
}
.msg.assistant a.path-link { text-decoration: none; }
.msg.assistant a.path-link code { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.msg.assistant pre {
  margin: 6px 0 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  overflow-x: auto;
}
.msg.assistant pre code { background: none; padding: 0; white-space: pre; }
.msg.assistant table { border-collapse: collapse; font-size: 13.5px; display: block; overflow-x: auto; margin: 6px 0; }
.msg.assistant td, .msg.assistant th { border: 1px solid var(--line); padding: 4px 8px; }

.thinking {
  font-size: 13px;
  color: var(--muted);
  border-left: 2px solid var(--line);
  padding-left: 10px;
}
.thinking summary { cursor: pointer; list-style: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking .t-body { white-space: pre-wrap; margin-top: 4px; max-height: 260px; overflow-y: auto; }

.tool {
  font-size: 13px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.tool summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  list-style: none;
  cursor: pointer;
}
.tool summary::-webkit-details-marker { display: none; }
.tool .name { font-weight: 600; color: #c6cad6; flex: none; }
.tool .detail { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.tool pre {
  margin: 0;
  padding: 8px 10px;
  border-top: 1px solid var(--line);
  max-height: 280px;
  overflow: auto;
  font: 12px/1.4 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
  color: #b7bbc8;
}
.tool.error { border-color: #5a2e33; }

.tool .dl { margin-left: auto; flex: none; display: grid; place-items: center; width: 28px; height: 22px; color: var(--accent); }
.tool .dl svg { width: 16px; height: 16px; }

a.item { color: inherit; text-decoration: none; }
.fc-icon { width: 22px; height: 22px; flex: none; color: var(--muted); margin-top: 1px; }
.fc-icon.file { color: var(--accent); }
.fc-icon svg { width: 22px; height: 22px; }

.file-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.file-card img { display: block; width: 100%; max-height: 320px; object-fit: contain; background: #000; }
.fc-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.fc-body { flex: 1; min-width: 0; }
.fc-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fc-meta { font-size: 12.5px; color: var(--muted); }
.fc-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  flex: none;
}
.fc-btn svg { width: 20px; height: 20px; }

.note, .end { font-size: 12.5px; color: var(--muted); text-align: center; }
.end.error { color: var(--err); }

.typing { display: flex; gap: 4px; padding: 4px 2px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: pulse 1s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }

/* ---------- composer ---------- */

.composer {
  border-top: 1px solid var(--line);
  background: var(--bg);
  padding: 8px 10px calc(8px + var(--safe-bottom));
}
.composer .thumbs { display: flex; gap: 6px; margin-bottom: 6px; flex-wrap: wrap; }
.composer .thumbs img { width: 52px; height: 52px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.composer .row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 40dvh;
  padding: 10px 12px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--panel);
  outline: none;
  font-size: 16px;
}
.composer textarea:focus { border-color: #3a4050; }
.send {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  flex: none;
}
.send.stop { background: var(--err); }
.send:disabled { opacity: 0.4; }
.send svg { width: 20px; height: 20px; }

.chips { display: flex; gap: 6px; padding: 0 2px 6px; overflow-x: auto; }
.chip {
  flex: none;
  font-size: 12.5px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: #c6cad6;
  max-width: 70vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- forms ---------- */

.form { padding: 14px 14px calc(24px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.input, .select, .textarea {
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  outline: none;
  font-size: 16px;
}
.textarea { min-height: 140px; resize: vertical; }
.btn {
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  text-align: center;
}
.btn.ghost { background: var(--panel); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--err); border: 1px solid #5a2e33; }

.ws-list { display: flex; flex-direction: column; gap: 6px; }
.ws {
  text-align: left;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid transparent;
  font-size: 14px;
  overflow-wrap: anywhere;
}
.ws.active { border-color: var(--accent); background: var(--panel-2); }

.login { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 24px; gap: 14px; max-width: 420px; width: 100%; margin: 0 auto; }
.login h1 { margin: 0 0 6px; font-size: 24px; }
.login p { margin: 0; color: var(--muted); }
.error-text { color: var(--err); font-size: 14px; min-height: 20px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  max-width: 90vw;
  z-index: 50;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

.offline-banner {
  background: #3a2c12;
  color: var(--warn);
  font-size: 13px;
  text-align: center;
  padding: 4px;
}

@media (min-width: 760px) {
  #app { max-width: 760px; margin: 0 auto; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .fab { right: calc(50% - 380px + 16px); }
}
