:root {
  --primary: #0F172A;
  --accent: #3B82F6;
  --accent-soft: #EFF6FF;
  --text: #334155;
  --muted: #94A3B8;
  --divider: #E2E8F0;
  --bg: #F8FAFC;
  --error: #B45309;
  --error-bg: #FEF3C7;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

/* Top bar */
.topbar {
  background: #fff;
  border-bottom: 1px solid var(--divider);
  padding: 14px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand {
  font-weight: 700;
  color: var(--primary);
  font-size: 13px;
  letter-spacing: 0.2em;
  text-decoration: none;
}
.ctx {
  color: var(--muted);
  font-size: 13px;
}

/* Chat */
main#chat {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  scroll-behavior: smooth;
}

.bubble {
  max-width: 75%;
  padding: 12px 16px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
  font-size: 16px;
}
.bubble.agent {
  align-self: flex-start;
  background: var(--accent-soft);
  color: var(--primary);
  border-bottom-left-radius: 4px;
}
.bubble.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.bubble.tool {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-style: italic;
  font-size: 13px;
  padding: 4px 0;
}
.bubble.error {
  align-self: center;
  background: var(--error-bg);
  color: var(--error);
  font-size: 14px;
}

/* Typing indicator */
.typing[hidden] { display: none; }
.typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  background: var(--accent-soft);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  margin: 4px 16px 0;
  max-width: 60px;
}
.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.4;
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

/* Composer */
#composer {
  position: sticky;
  bottom: 0;
  background: #fff;
  border-top: 1px solid var(--divider);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  align-items: flex-end;
}
#input {
  flex: 1;
  resize: none;
  border: 1px solid var(--divider);
  background: #F8FAFC;
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
  color: var(--primary);
  line-height: 1.4;
  max-height: 160px;
  min-height: 44px;
  outline: none;
  transition: border-color 0.12s ease;
}
#input:focus { border-color: var(--accent); background: #fff; }

#send {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s ease, opacity 0.12s ease;
}
#send:hover:not(:disabled) { background: #2563EB; }
#send:disabled { opacity: 0.4; cursor: not-allowed; }

/* Success card */
.success-card[hidden] { display: none; }
.success-card {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 20;
}
.success-inner {
  background: #fff;
  border: 1px solid var(--divider);
  border-radius: 16px;
  padding: 48px 32px;
  max-width: 480px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(15, 23, 42, 0.06);
}
.check {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}
.success-inner h1 {
  color: var(--primary);
  font-size: 28px;
  font-weight: 700;
  margin: 0 0 12px;
}
.success-inner p {
  color: var(--text);
  margin: 0 0 28px;
}
#restart {
  background: var(--primary);
  color: #fff;
  border: 0;
  padding: 12px 24px;
  border-radius: 8px;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s ease;
}
#restart:hover { background: #1E293B; }
.success-inner .ref {
  color: var(--muted);
  font-size: 12px;
  margin-top: 28px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
}

/* Mobile tweaks */
@media (max-width: 540px) {
  main#chat { padding: 16px 12px 4px; }
  .bubble { max-width: 85%; font-size: 15px; }
}
