:root {
  --bg: #f7f5f1;
  --panel: #ffffff;
  --text: #1f1d1a;
  --muted: #6f6a62;
  --line: #e4dfd6;
  --accent: #d97757;
  --accent-text: #ffffff;
  --user: #ece6dc;
  --error: #b3261e;
  --ok: #2e7d32;
  --warn: #b26a00;
  --code: #f0ece5;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f1d1a;
    --panel: #2a2824;
    --text: #ece8e1;
    --muted: #a39e95;
    --line: #3a3732;
    --user: #3a3631;
    --code: #34312c;
    --error: #f2b8b5;
    --ok: #8bc48e;
    --warn: #f0b860;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

[hidden] {
  display: none !important;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

code,
pre {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.88em;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

.identity {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.logo {
  width: 30px;
  height: 30px;
}

.title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.subtitle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.dot.online {
  background: var(--ok);
}

.dot.busy {
  background: var(--warn);
}

.dot.offline {
  background: var(--error);
}

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
}

.icon-button:hover {
  background: var(--user);
}

.icon-button svg,
.send svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.icon-button circle {
  fill: currentColor;
  stroke: none;
}

.onboarding {
  max-width: 560px;
  margin: 40px auto;
  padding: 0 20px;
  line-height: 1.5;
}

.onboarding h1 {
  font-size: 1.5rem;
}

.onboarding code {
  background: var(--code);
  padding: 2px 6px;
  border-radius: 6px;
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

.log {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 320px;
  line-height: 1.5;
}

.bubble {
  max-width: min(760px, 92%);
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.bubble.user {
  align-self: flex-end;
  background: var(--user);
  white-space: pre-wrap;
}

.bubble.assistant,
.bubble.error,
.bubble.pending {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--line);
}

.bubble.error {
  border-color: var(--error);
  color: var(--error);
  white-space: pre-wrap;
}

.bubble p {
  margin: 0 0 0.6em;
}

.bubble p:last-child {
  margin-bottom: 0;
}

.bubble pre {
  background: var(--code);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre;
}

.bubble :not(pre) > code {
  background: var(--code);
  padding: 1px 5px;
  border-radius: 5px;
}

.meta {
  margin-top: 6px;
  font-size: 0.75rem;
  color: var(--muted);
}

details.steps {
  margin-bottom: 8px;
  font-size: 0.82rem;
  color: var(--muted);
}

details.steps summary {
  cursor: pointer;
}

details.steps ol {
  margin: 6px 0 0;
  padding-left: 18px;
}

details.steps li {
  margin: 2px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow-wrap: anywhere;
}

.working {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.9rem;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.mfa {
  margin: 0 12px 8px;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--warn);
  background: var(--panel);
  display: grid;
  gap: 8px;
  font-size: 0.92rem;
}

.mfa form {
  display: flex;
  gap: 8px;
}

.mfa input {
  flex: 1;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  letter-spacing: 0.2em;
  font-size: 1.1rem;
}

.mfa button,
.menu-actions button {
  padding: 10px 14px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}

.composer {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 40dvh;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  line-height: 1.4;
}

.composer textarea:focus,
.mfa input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.attach {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.attach:hover {
  background: var(--user);
}

.attach svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.attachments {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 10px 0;
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.attachment {
  position: relative;
  flex: none;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
}

.attachment img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.attachment button {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 50%;
  background: rgb(0 0 0 / 60%);
  color: #fff;
  font-size: 12px;
  line-height: 22px;
  padding: 0;
  cursor: pointer;
}

.upload-status {
  align-self: center;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
}

.thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}

.thumbs a,
.thumbs .thumb-missing {
  display: block;
  width: 120px;
  height: 120px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--code);
}

.thumbs img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumbs .thumb-missing {
  display: grid;
  place-items: center;
  padding: 6px;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  overflow-wrap: anywhere;
}

body.dragging .composer {
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
}

.send,
.stop {
  height: 44px;
  border: 0;
  border-radius: 12px;
  cursor: pointer;
}

.send {
  width: 44px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--accent-text);
}

.send:disabled {
  opacity: 0.4;
  cursor: default;
}

.stop {
  padding: 0 14px;
  background: var(--user);
}

.drawer {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 10;
  width: min(340px, 88vw);
  background: var(--panel);
  border-right: 1px solid var(--line);
  box-shadow: 0 0 40px rgb(0 0 0 / 25%);
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 10px 16px;
  border-bottom: 1px solid var(--line);
}

#chat-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
}

#chat-list li {
  display: flex;
  align-items: center;
  gap: 4px;
}

#chat-list .open-chat {
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
}

#chat-list .open-chat.active,
#chat-list .open-chat:hover {
  background: var(--user);
}

#chat-list .chat-title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#chat-list .chat-meta {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
}

#chat-list .delete-chat {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 8px;
  cursor: pointer;
  border-radius: 8px;
}

dialog {
  width: min(460px, 92vw);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
}

dialog::backdrop {
  background: rgb(0 0 0 / 40%);
}

dialog h2 {
  margin-top: 0;
  font-size: 1.1rem;
}

dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  font-size: 0.9rem;
}

dt {
  color: var(--muted);
}

dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.menu-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.menu-actions .danger {
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
}
