:root {
  --bg: #0f1115;
  --bg-soft: #161a21;
  --panel: #1b2029;
  --panel-2: #222834;
  --text: #e8ecf2;
  --muted: #8b95a6;
  --accent: #6ea8fe;
  --accent-2: #8b5cf6;
  --user: #23324a;
  --danger: #ff6b6b;
  --radius: 18px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; font-size: 14px; }

/* ---------- login ---------- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #1c2740 0%, var(--bg) 60%);
}
.login form {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login h1 { margin: 0; font-size: 24px; font-weight: 650; letter-spacing: -0.02em; }
.login p { margin: 0; }
.logo {
  width: 64px; height: 64px;
  margin: 0 auto 4px;
  border-radius: 20px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 28px;
  color: #fff;
  background: linear-gradient(140deg, var(--accent), var(--accent-2));
  box-shadow: 0 12px 40px rgba(110, 168, 254, 0.25);
}
.logo.small { width: 44px; height: 44px; border-radius: 14px; font-size: 20px; margin-bottom: 10px; }

.login input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid #2a3140;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.login input:focus { border-color: var(--accent); }
.login button {
  padding: 14px 16px;
  border: 0;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 600;
  color: #0b1020;
  background: linear-gradient(135deg, var(--accent), #9cc0ff);
  cursor: pointer;
}
.login button:active { transform: scale(0.99); }

/* ---------- app ---------- */
.app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: rgba(15, 17, 21, 0.85);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid #202632;
  position: sticky;
  top: 0;
  z-index: 5;
}
.identity { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #4ade80;
  box-shadow: 0 0 0 4px rgba(74, 222, 128, 0.12);
}
.dot.busy { background: var(--accent); box-shadow: 0 0 0 4px rgba(110, 168, 254, 0.15); animation: pulse 1.2s infinite; }
.dot.off { background: var(--danger); box-shadow: 0 0 0 4px rgba(255, 107, 107, 0.15); }
@keyframes pulse { 50% { opacity: 0.45; } }

.actions { display: flex; gap: 6px; }
.ghost {
  width: 40px; height: 40px;
  border-radius: 12px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.ghost svg {
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ghost:hover { background: var(--panel); color: var(--text); }

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

.welcome {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 320px;
}
.welcome p { margin: 0; font-size: 15px; }

.msg {
  max-width: min(720px, 88%);
  padding: 11px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  animation: rise 0.18s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(4px); } }

.msg.user {
  align-self: flex-end;
  background: var(--user);
  border-bottom-right-radius: 8px;
}
.msg.assistant {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid #242b38;
  border-bottom-left-radius: 8px;
}
.msg.system {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  padding: 2px;
}
.msg.error {
  align-self: flex-start;
  border: 1px solid rgba(255, 107, 107, 0.4);
  color: #ffd7d7;
  background: rgba(255, 107, 107, 0.08);
}

.msg pre {
  margin: 8px 0 2px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #0c0f14;
  border: 1px solid #242b38;
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.4;
}
.msg code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13.5px;
}
.msg :not(pre) > code {
  background: #0c0f14;
  border: 1px solid #242b38;
  padding: 1px 6px;
  border-radius: 7px;
}
.msg a { color: var(--accent); }
.msg.assistant.typing::after {
  content: "▉";
  color: var(--accent);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.cmds {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(76px + var(--safe-bottom));
  max-height: 45vh;
  overflow-y: auto;
  padding: 6px;
  border-radius: 16px;
  background: #181d26;
  border: 1px solid #2a3140;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
  z-index: 20;
}
.cmd {
  display: flex;
  align-items: baseline;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 11px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.cmd:hover, .cmd.active { background: var(--panel); }
.cmd b { font-weight: 600; }
.cmd span { color: var(--muted); font-size: 13.5px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(12px + var(--safe-bottom));
  background: rgba(15, 17, 21, 0.92);
  backdrop-filter: blur(14px);
  border-top: 1px solid #202632;
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  min-height: 46px;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid #2a3140;
  background: var(--bg-soft);
  color: var(--text);
  font: inherit;
  line-height: 1.35;
  outline: none;
}
.composer textarea:focus { border-color: #3b4a63; }
.send {
  width: 46px; height: 46px;
  flex: 0 0 46px;
  border: 0;
  border-radius: 50%;
  font-size: 20px;
  font-weight: 700;
  color: #0b1020;
  background: linear-gradient(135deg, var(--accent), #9cc0ff);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.send svg {
  width: 22px; height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.send.stop svg { fill: currentColor; stroke: none; }
.send:disabled { opacity: 0.4; }
.send.stop { color: #fff; background: #3a2530; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; scroll-behavior: auto !important; }
}
