:root {
  color-scheme: dark;
  --bg: #090d16;
  --panel: #111827;
  --panel-2: #182235;
  --line: #27344a;
  --text: #f4f7fb;
  --muted: #94a3b8;
  --accent: #71e0b5;
  --accent-strong: #37c98e;
  --danger: #ff8e92;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { overscroll-behavior: none; }
button, input, textarea { font: inherit; }
button { color: inherit; }
.hidden { display: none !important; }

.auth-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 18% 16%, rgba(55, 201, 142, .18), transparent 34%),
    radial-gradient(circle at 85% 80%, rgba(73, 112, 255, .16), transparent 36%),
    var(--bg);
}

.auth-card {
  width: min(420px, 100%);
  padding: 30px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: rgba(17, 24, 39, .92);
  box-shadow: 0 30px 90px rgba(0, 0, 0, .35);
}

.brand-mark {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: var(--accent);
  color: #05291d;
  font-weight: 900;
  font-size: 24px;
}

.auth-card h1 { margin: 20px 0 8px; font-size: 28px; }
.auth-card p { margin: 0 0 24px; color: var(--muted); line-height: 1.6; }
.auth-card label { display: block; margin-bottom: 8px; color: var(--muted); font-size: 13px; }
.auth-card input, .search-wrap input, .composer textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #0c1320;
  color: var(--text);
  outline: none;
}
.auth-card input { height: 50px; padding: 0 14px; }
.auth-card input:focus, .search-wrap input:focus, .composer textarea:focus { border-color: var(--accent-strong); }
.auth-card button {
  width: 100%;
  height: 50px;
  margin-top: 14px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #05291d;
  font-weight: 800;
}
.error { min-height: 22px; margin-top: 12px; color: var(--danger); font-size: 13px; }

.app-shell { height: 100dvh; display: grid; grid-template-rows: auto 1fr; overflow: hidden; }
.topbar {
  min-height: 68px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(9, 13, 22, .94);
  backdrop-filter: blur(18px);
  z-index: 5;
}
.topbar h1 { margin: 1px 0 0; max-width: 62vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 18px; }
.eyebrow { color: var(--muted); font-size: 9px; font-weight: 800; letter-spacing: .16em; }
.icon-button {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel);
  font-size: 21px;
}
.connection { margin-left: auto; color: var(--accent); font-size: 12px; white-space: nowrap; }
.connection span { display: inline-block; width: 7px; height: 7px; margin-right: 6px; border-radius: 50%; background: currentColor; }
.connection.offline { color: var(--danger); }
main { min-height: 0; overflow: hidden; }

.thread-list-view { height: 100%; overflow-y: auto; padding: 14px 14px calc(28px + var(--safe-bottom)); }
.search-wrap { display: flex; gap: 9px; margin-bottom: 12px; }
.search-wrap input { height: 44px; padding: 0 14px; }
.threads { display: grid; gap: 8px; transition: opacity .2s; }
.threads.loading { opacity: .5; }
.thread-card {
  width: 100%;
  padding: 15px;
  display: grid;
  gap: 10px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
}
.thread-card:active { transform: scale(.99); background: var(--panel-2); }
.thread-card strong { font-size: 15px; line-height: 1.35; }
.thread-card-meta { display: flex; gap: 9px; color: var(--muted); font-size: 12px; }
.thread-card-meta time { margin-left: auto; }
.pin { color: var(--accent); }
.load-more { width: 100%; margin-top: 14px; padding: 13px; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); }
.empty { padding: 60px 20px; text-align: center; color: var(--muted); }

.thread-view { height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr) auto auto; }
.thread-meta { min-height: 38px; padding: 8px 16px; display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; border-bottom: 1px solid var(--line); }
.status { margin-left: auto; padding: 4px 9px; border-radius: 999px; background: var(--panel-2); }
.status.running { color: var(--accent); }
.messages { overflow-y: auto; padding: 18px 14px 28px; scroll-behavior: smooth; }
.message { max-width: 92%; margin-bottom: 18px; }
.message.user { margin-left: auto; }
.message-label { margin: 0 7px 6px; color: var(--muted); font-size: 11px; }
.message.user .message-label { text-align: right; }
.message-text {
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 16px 16px 16px 5px;
  background: var(--panel);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.55;
  font-size: 14px;
}
.message.user .message-text { border-color: rgba(55, 201, 142, .35); border-radius: 16px 16px 5px 16px; background: rgba(28, 78, 63, .42); }
.message.commentary .message-text { color: #cbd5e1; background: #0d1523; }
.queue { max-height: 96px; overflow-y: auto; padding: 8px 14px; border-top: 1px solid var(--line); color: #facc75; font-size: 12px; background: rgba(98, 68, 15, .18); }
.queue div + div { margin-top: 5px; }
.composer {
  padding: 10px 12px calc(10px + var(--safe-bottom));
  display: flex;
  align-items: flex-end;
  gap: 9px;
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.composer textarea { min-height: 46px; max-height: 180px; resize: none; padding: 12px 13px; line-height: 1.45; }
.composer button { width: 46px; height: 46px; flex: 0 0 auto; border: 0; border-radius: 14px; background: var(--accent); color: #05291d; font-size: 22px; font-weight: 900; }
.composer button:disabled { opacity: .45; }
.toast { position: fixed; left: 50%; bottom: calc(82px + var(--safe-bottom)); transform: translateX(-50%); max-width: calc(100% - 32px); padding: 11px 15px; border-radius: 12px; background: #d8fff0; color: #073523; box-shadow: 0 12px 50px rgba(0,0,0,.4); font-size: 13px; z-index: 20; }
.toast.error-toast { background: #ffe0e1; color: #5d1518; }

@media (min-width: 820px) {
  .app-shell { grid-template-columns: 360px 1fr; grid-template-rows: auto 1fr; }
  .topbar { grid-column: 1 / -1; }
  main { grid-column: 1 / -1; display: grid; grid-template-columns: 360px 1fr; }
  .thread-list-view { display: block !important; border-right: 1px solid var(--line); }
  .thread-view { display: grid !important; }
  #back { display: none !important; }
  .thread-card { border-radius: 12px; }
}

