* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: #f0f2f5;
  color: #1c1e21;
}

.app {
  max-width: 780px;
  margin: 0 auto;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #fff;
  box-shadow: 0 0 24px rgba(0, 0, 0, 0.08);
}

header {
  padding: 16px 20px;
  border-bottom: 1px solid #e4e6eb;
  background: #16324f;
  color: #fff;
}

header h1 { font-size: 1.15rem; }
header p { font-size: 0.8rem; opacity: 0.8; margin-top: 4px; }

main {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  font-size: 0.92rem;
}

.msg.user,
.msg.pending,
.msg.error { white-space: pre-wrap; }

.msg.user {
  align-self: flex-end;
  background: #16324f;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  align-self: flex-start;
  max-width: 92%;
  background: #f0f2f5;
  border-bottom-left-radius: 4px;
}

.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0.65em 0; }
.msg.assistant ul,
.msg.assistant ol { margin: 0.65em 0; padding-left: 1.4rem; }
.msg.assistant li + li { margin-top: 0.25em; }
.msg.assistant h1,
.msg.assistant h2,
.msg.assistant h3,
.msg.assistant h4 { margin: 0.9em 0 0.4em; line-height: 1.25; }
.msg.assistant h1 { font-size: 1.2rem; }
.msg.assistant h2 { font-size: 1.1rem; }
.msg.assistant h3,
.msg.assistant h4 { font-size: 1rem; }
.msg.assistant blockquote {
  margin: 0.7em 0;
  padding-left: 0.8rem;
  border-left: 3px solid #9aabba;
  color: #40546a;
}
.msg.assistant code {
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: #dfe5eb;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.85em;
}
.msg.assistant pre {
  margin: 0.7em 0;
  padding: 0.75rem;
  overflow-x: auto;
  border-radius: 8px;
  background: #dfe5eb;
}
.msg.assistant pre code { padding: 0; background: transparent; }
.msg.assistant a { color: #135f96; text-underline-offset: 2px; }
.msg.assistant table {
  display: block;
  width: max-content;
  max-width: 100%;
  margin: 0.8em 0;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: 0.86rem;
}
.msg.assistant th,
.msg.assistant td {
  padding: 0.5rem 0.65rem;
  border: 1px solid #c7d0d9;
  text-align: left;
  vertical-align: top;
}
.msg.assistant th { background: #dce4eb; font-weight: 700; }
.msg.assistant tbody tr:nth-child(even) { background: rgba(255, 255, 255, 0.45); }

.msg.error {
  align-self: flex-start;
  background: #fdecea;
  color: #8a1c12;
}

.msg.pending { opacity: 0.6; font-style: italic; }

.tool-event {
  align-self: flex-start;
  max-width: 85%;
  font-size: 0.8rem;
  border: 1px dashed #b7c4d1;
  border-radius: 10px;
  padding: 6px 10px;
  background: #f8fafc;
  color: #40546a;
}

.tool-event.error { border-color: #d9a0a0; background: #fdf5f5; }

.tool-event summary { cursor: pointer; font-weight: 600; }

.tool-event pre {
  margin-top: 6px;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 0.75rem;
  background: #eef2f6;
  padding: 6px 8px;
  border-radius: 6px;
}

footer {
  padding: 14px 20px;
  border-top: 1px solid #e4e6eb;
}

#chat-form { display: flex; gap: 10px; }

#chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #ccd3db;
  border-radius: 22px;
  font-size: 0.92rem;
  outline: none;
}

#chat-input:focus { border-color: #16324f; }

#send-btn {
  padding: 10px 22px;
  border: none;
  border-radius: 22px;
  background: #16324f;
  color: #fff;
  font-size: 0.92rem;
  cursor: pointer;
}

#send-btn:disabled { opacity: 0.5; cursor: wait; }
