* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: #f7f7f8;
  color: #222;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: #fff;
  border-bottom: 1px solid #e5e5e7;
}

header h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.status {
  font-size: 0.8rem;
  color: #888;
}

.status.ready { color: #2a8a3e; }
.status.error { color: #c03030; }

#session-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  background: #fafafa;
  border-bottom: 1px solid #e5e5e7;
  font-size: 0.85rem;
}

#session-bar label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

#session-bar select {
  padding: 0.3rem 0.5rem;
  font: inherit;
  border: 1px solid #d4d4d8;
  border-radius: 0.35rem;
  background: #fff;
}

#session-bar .badge {
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  background: #eee;
  color: #555;
  border: 1px solid #ddd;
}

#session-bar .badge.synced {
  background: #e8f5ec;
  color: #1f6b32;
  border-color: #c2e1cd;
}

#session-bar .badge.unsynced {
  background: #fdecec;
  color: #8b1f1f;
  border-color: #f3c2c2;
}

#session-bar .badge.syncing {
  background: #fff5d6;
  color: #8b6b1f;
  border-color: #f0dca0;
}

#sync-btn {
  margin-left: auto;
  padding: 0.35rem 0.85rem;
  font: inherit;
  background: #fff;
  color: #2563eb;
  border: 1px solid #2563eb;
  border-radius: 0.35rem;
  cursor: pointer;
}

#sync-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#messages {
  list-style: none;
  margin: 0;
  padding: 1rem;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#messages li {
  max-width: 70ch;
  padding: 0.6rem 0.85rem;
  border-radius: 0.75rem;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.4;
}

#messages li.user {
  align-self: flex-end;
  background: #2563eb;
  color: #fff;
}

#messages li.assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid #e5e5e7;
}

#messages li.error {
  align-self: stretch;
  background: #fdecec;
  color: #8b1f1f;
  border: 1px solid #f3c2c2;
  font-size: 0.85rem;
}

#messages li.phase {
  align-self: stretch;
  background: transparent;
  color: #6b7280;
  border: 1px dashed #e5e7eb;
  font-size: 0.8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 0.35rem 0.6rem;
}

#messages li.phase.done {
  color: #166534;
  border-color: #bbf7d0;
}

#messages li.phase.err {
  color: #8b1f1f;
  border-color: #f3c2c2;
}

#messages li.phase .phase-name {
  font-weight: 600;
  margin-right: 0.4rem;
}

#messages li.assistant { line-height: 1.35; }
#messages li.assistant > *:first-child { margin-top: 0; }
#messages li.assistant > *:last-child { margin-bottom: 0; }
#messages li.assistant p,
#messages li.assistant ul,
#messages li.assistant ol { margin: 0.25rem 0; }
#messages li.assistant li { margin: 0.05rem 0; }
#messages li.assistant li > p { margin: 0; }
#messages li.assistant ul,
#messages li.assistant ol { padding-left: 1.25rem; }
#messages li.assistant h1,
#messages li.assistant h2,
#messages li.assistant h3,
#messages li.assistant h4 { margin: 0.5rem 0 0.2rem; line-height: 1.2; }
#messages li.assistant h1 { font-size: 1.15rem; }
#messages li.assistant h2 { font-size: 1.05rem; }
#messages li.assistant h3 { font-size: 0.98rem; }
#messages li.assistant code {
  background: #f1f1f3;
  padding: 0.05rem 0.3rem;
  border-radius: 0.25rem;
  font-size: 0.88em;
}
#messages li.assistant pre {
  background: #f4f4f6;
  border: 1px solid #e5e5e7;
  padding: 0.6rem 0.75rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  margin: 0.4rem 0;
}
#messages li.assistant pre code {
  background: transparent;
  padding: 0;
}
#messages li.assistant a { color: #2563eb; }
#messages li.assistant table {
  border-collapse: collapse;
  margin: 0.4rem 0;
}
#messages li.assistant th,
#messages li.assistant td {
  border: 1px solid #e5e5e7;
  padding: 0.3rem 0.55rem;
  text-align: left;
}

#messages li.typing {
  display: flex;
  gap: 0.25rem;
  padding: 0.7rem 0.85rem;
}

#messages li.typing span {
  display: inline-block;
  width: 0.45rem;
  height: 0.45rem;
  background: #888;
  border-radius: 50%;
  animation: typing-blink 1.2s infinite ease-in-out;
}

#messages li.typing span:nth-child(2) { animation-delay: 0.2s; }
#messages li.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

#composer {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid #e5e5e7;
  background: #fff;
}

#input {
  flex: 1;
  padding: 0.6rem 0.85rem;
  font: inherit;
  border: 1px solid #d4d4d8;
  border-radius: 0.5rem;
  outline: none;
}

#input:focus { border-color: #2563eb; }

#send {
  padding: 0.6rem 1.1rem;
  font: inherit;
  background: #2563eb;
  color: #fff;
  border: none;
  border-radius: 0.5rem;
  cursor: pointer;
}

#send:disabled, #input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
